文件下载:

html:

<html>
<body>
<a href="1.rar">下载1.rar</a>
<br />
<a href="1.jpg">下载1.jpg</a> <!--会显示文件内容,而不是下载-->
<br />
<a href="doDownload.php?filename=1.jpg">通过程序下载1.jpg</a>
<br />
<a href="doDownload.php?filename=../upload/nv.jpg">下载nv.jpg</a>
</body>
</html>

php处理:

<?php
$filename=$_GET['filename'];
//设置下载文件名
header('content-disposition:attachment;filename='.basename($filename));
header('content-length:'.filesize($filename));
readfile($filename);

文件上传:

html代码:

<html>
<body>
<form action="doAction5.php" method="post" enctype="multipart/form-data">
请选择您要上传的文件:<input type="file" name='myFile1' /><br/>
请选择您要上传的文件:<input type="file" name='myFile2' /><br/>
请选择您要上传的文件:<input type="file" name='myFile[]' /><br/>
请选择您要上传的文件:<input type="file" name='myFile[]' /><br/>
请选择您要上传的文件:<input type="file" name='myFile[]' multiple="multiple" /><br/>
<input type="submit" value="上传文件" />
</form>
</body>
</html>

php代码:

<?php
/**
* 得到文件扩展名
* @param string $filename
* @return string
*/
function getExt($filename){
return strtolower(pathinfo($filename,PATHINFO_EXTENSION));
} /**
* 产生唯一字符串
* @return string
*/
function getUniName(){
return md5(uniqid(microtime(true),true));
}

upload.func1.php

<?php 

/**
* 构建上传文件信息
* @return unknown
*/
function getFiles(){
$i=0;
foreach($_FILES as $file){
if(is_string($file['name'])){
$files[$i]=$file;
$i++;
}elseif(is_array($file['name'])){
foreach($file['name'] as $key=>$val){
$files[$i]['name']=$file['name'][$key];
$files[$i]['type']=$file['type'][$key];
$files[$i]['tmp_name']=$file['tmp_name'][$key];
$files[$i]['error']=$file['error'][$key];
$files[$i]['size']=$file['size'][$key];
$i++;
}
}
}
return $files; }
/**
* 针对于单文件、多个单文件、多文件的上传
* @param array $fileInfo
* @param string $path
* @param string $flag
* @param number $maxSize
* @param array $allowExt
* @return string
*/
function uploadFile($fileInfo,$path='./uploads',$flag=true,$maxSize=1048576,$allowExt=array('jpeg','jpg','png','gif')){
//$flag=true;
//$allowExt=array('jpeg','jpg','gif','png');
//$maxSize=1048576;//1M
//判断错误号
if($fileInfo['error']===UPLOAD_ERR_OK){
//检测上传得到小
if($fileInfo['size']>$maxSize){
$res['mes']=$fileInfo['name'].'上传文件过大';
}
$ext=getExt($fileInfo['name']);
//检测上传文件的文件类型
if(!in_array($ext,$allowExt)){
$res['mes']=$fileInfo['name'].'非法文件类型';
}
//检测是否是真实的图片类型
if($flag){
if(!getimagesize($fileInfo['tmp_name'])){
$res['mes']=$fileInfo['name'].'不是真实图片类型';
}
}
//检测文件是否是通过HTTP POST上传上来的
if(!is_uploaded_file($fileInfo['tmp_name'])){
$res['mes']=$fileInfo['name'].'文件不是通过HTTP POST方式上传上来的';
}
if($res) return $res;
//$path='./uploads';
if(!file_exists($path)){
mkdir($path,0777,true);
chmod($path,0777);
}
$uniName=getUniName();
$destination=$path.'/'.$uniName.'.'.$ext;
if(!move_uploaded_file($fileInfo['tmp_name'],$destination)){
$res['mes']=$fileInfo['name'].'文件移动失败';
}
$res['mes']=$fileInfo['name'].'上传成功';
$res['dest']=$destination;
return $res; }else{
//匹配错误信息
switch ($fileInfo ['error']) {
case 1 :
$res['mes'] = '上传文件超过了PHP配置文件中upload_max_filesize选项的值';
break;
case 2 :
$res['mes'] = '超过了表单MAX_FILE_SIZE限制的大小';
break;
case 3 :
$res['mes'] = '文件部分被上传';
break;
case 4 :
$res['mes'] = '没有选择上传文件';
break;
case 6 :
$res['mes'] = '没有找到临时目录';
break;
case 7 :
case 8 :
$res['mes'] = '系统错误';
break;
}
return $res;
}
}

doAction5.php

<?php
//print_r($_FILES);
header("content-type:text/html;charset=utf-8");
require_once 'upload.func1.php';
require_once 'common.func.php';
$files=getFiles();
// print_r($files);
foreach($files as $fileInfo){
$res=uploadFile($fileInfo);
echo $res['mes'],'<br/>';
$uploadFiles[]=$res['dest'];
}
$uploadFiles=array_values(array_filter($uploadFiles));
print_r($uploadFiles);

上面是通过函数实现,下载封装成为类:

html:

<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form action="doAction6.php" method="post" enctype="multipart/form-data">
请选择您要上传的文件:<input type="file" name='myFile1' />
<input type="submit" value="上传文件" />
</form>
</body>
</html>

upload.class.php

<?php
class upload{
protected $fileName;
protected $maxSize;
protected $allowMime;
protected $allowExt;
protected $uploadPath;
protected $imgFlag;
protected $fileInfo;
protected $error;
protected $ext;
/**
* @param string $fileName
* @param string $uploadPath
* @param string $imgFlag
* @param number $maxSize
* @param array $allowExt
* @param array $allowMime
*/
public function __construct($fileName='myFile',$uploadPath='./uploads',$imgFlag=true,$maxSize=5242880,$allowExt=array('jpeg','jpg','png','gif'),$allowMime=array('image/jpeg','image/png','image/gif')){
$this->fileName=$fileName;
$this->maxSize=$maxSize;
$this->allowMime=$allowMime;
$this->allowExt=$allowExt;
$this->uploadPath=$uploadPath;
$this->imgFlag=$imgFlag;
$this->fileInfo=$_FILES[$this->fileName];
}
/**
* 检测上传文件是否出错
* @return boolean
*/
protected function checkError(){
if(!is_null($this->fileInfo)){
if($this->fileInfo['error']>0){
switch($this->fileInfo['error']){
case 1:
$this->error='超过了PHP配置文件中upload_max_filesize选项的值';
break;
case 2:
$this->error='超过了表单中MAX_FILE_SIZE设置的值';
break;
case 3:
$this->error='文件部分被上传';
break;
case 4:
$this->error='没有选择上传文件';
break;
case 6:
$this->error='没有找到临时目录';
break;
case 7:
$this->error='文件不可写';
break;
case 8:
$this->error='由于PHP的扩展程序中断文件上传';
break; }
return false;
}else{
return true;
}
}else{
$this->error='文件上传出错';
return false;
}
}
/**
* 检测上传文件的大小
* @return boolean
*/
protected function checkSize(){
if($this->fileInfo['size']>$this->maxSize){
$this->error='上传文件过大';
return false;
}
return true;
}
/**
* 检测扩展名
* @return boolean
*/
protected function checkExt(){
$this->ext=strtolower(pathinfo($this->fileInfo['name'],PATHINFO_EXTENSION));
if(!in_array($this->ext,$this->allowExt)){
$this->error='不允许的扩展名';
return false;
}
return true;
}
/**
* 检测文件的类型
* @return boolean
*/
protected function checkMime(){
if(!in_array($this->fileInfo['type'],$this->allowMime)){
$this->error='不允许的文件类型';
return false;
}
return true;
}
/**
* 检测是否是真实图片
* @return boolean
*/
protected function checkTrueImg(){
if($this->imgFlag){
if(!@getimagesize($this->fileInfo['tmp_name'])){
$this->error='不是真实图片';
return false;
}
return true;
}
}
/**
* 检测是否通过HTTP POST方式上传上来的
* @return boolean
*/
protected function checkHTTPPost(){
if(!is_uploaded_file($this->fileInfo['tmp_name'])){
$this->error='文件不是通过HTTP POST方式上传上来的';
return false;
}
return true;
}
/**
*显示错误
*/
protected function showError(){
exit('<span style="color:red">'.$this->error.'</span>');
}
/**
* 检测目录不存在则创建
*/
protected function checkUploadPath(){
if(!file_exists($this->uploadPath)){
mkdir($this->uploadPath,0777,true);
}
}
/**
* 产生唯一字符串
* @return string
*/
protected function getUniName(){
return md5(uniqid(microtime(true),true));
}
/**
* 上传文件
* @return string
*/
public function uploadFile(){
if($this->checkError()&&$this->checkSize()&&$this->checkExt()&&$this->checkMime()&&$this->checkTrueImg()&&$this->checkHTTPPost()){
$this->checkUploadPath();
$this->uniName=$this->getUniName();
$this->destination=$this->uploadPath.'/'.$this->uniName.'.'.$this->ext;
if(@move_uploaded_file($this->fileInfo['tmp_name'], $this->destination)){
return $this->destination;
}else{
$this->error='文件移动失败';
$this->showError();
}
}else{
$this->showError();
}
}
}

doAction6.php

<?php
header('content-type:text/html;charset=utf-8');
require_once 'upload.class.php';
$upload=new upload('myFile1','imooc');
$dest=$upload->uploadFile();
echo $dest;

php 文件上传,下载的更多相关文章

  1. Struts的文件上传下载

    Struts的文件上传下载 1.文件上传 Struts2的文件上传也是使用fileUpload的组件,这个组默认是集合在框架里面的.且是使用拦截器:<interceptor name=" ...

  2. Android okHttp网络请求之文件上传下载

    前言: 前面介绍了基于okHttp的get.post基本使用(http://www.cnblogs.com/whoislcj/p/5526431.html),今天来实现一下基于okHttp的文件上传. ...

  3. Selenium2学习-039-WebUI自动化实战实例-文件上传下载

    通常在 WebUI 自动化测试过程中必然会涉及到文件上传的自动化测试需求,而开发在进行相应的技术实现是不同的,粗略可划分为两类:input标签类(类型为file)和非input标签类(例如:div.a ...

  4. 艺萌文件上传下载及自动更新系统(基于networkComms开源TCP通信框架)

    1.艺萌文件上传下载及自动更新系统,基于Winform技术,采用CS架构,开发工具为vs2010,.net2.0版本(可以很容易升级为3.5和4.0版本)开发语言c#. 本系统主要帮助客户学习基于TC ...

  5. 艺萌TCP文件上传下载及自动更新系统介绍(TCP文件传输)(一)

    艺萌TCP文件上传下载及自动更新系统介绍(TCP文件传输) 该系统基于开源的networkComms通讯框架,此通讯框架以前是收费的,目前已经免费并开元,作者是英国的,开发时间5年多,框架很稳定. 项 ...

  6. ssh框架文件上传下载

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. SpringMVC——返回JSON数据&&文件上传下载

    --------------------------------------------返回JSON数据------------------------------------------------ ...

  8. 【FTP】FTP文件上传下载-支持断点续传

    Jar包:apache的commons-net包: 支持断点续传 支持进度监控(有时出不来,搞不清原因) 相关知识点 编码格式: UTF-8等; 文件类型: 包括[BINARY_FILE_TYPE(常 ...

  9. NetworkComms 文件上传下载和客户端自动升级(非开源)

    演示程序下载地址:http://pan.baidu.com/s/1geVfmcr 淘宝地址:https://shop183793329.taobao.com 联系QQ号:3201175853 许可:购 ...

  10. SpringMVC文件上传下载

    在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/q ...

随机推荐

  1. linked-list-random-node

    https://leetcode.com/problems/linked-list-random-node/ // Using Reservoir sampling algorithm // http ...

  2. Python并发编程-Redis

    Redis 简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Remote Dictionary Server(Redis)是一个基于 key-value ...

  3. JNI 详细使用 基础【步骤】

    1.定义本地[native]方法.通常情况下,应单独定义一个类来封装所有native方法.native方法相当于一个[接口]中的方法,只有方法声明,没有方法体. 2.在项目根目录下创建[jni文件夹] ...

  4. Loadrunner视频教程汇总

    小布老师视频:测试工具概述,兼LoadRunner介绍 -1-4http://www.boobooke.com/v/bbk1046http://www.boobooke.com/v/bbk1046.z ...

  5. API手册 常用功能

    directive [ng] a form input input [checkbox] input [email] input [number] input [radio] input [text] ...

  6. NYOJ-267 郁闷的C小加(二)

    郁闷的C小加(二) 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 聪明的你帮助C小加解决了中缀表达式到后缀表达式的转换(详情请参考“郁闷的C小加(一)”),C小加很 ...

  7. hdu 4491 Windmill Animation

    A windmill animation works as follows: A two-dimensional set of points, no three of which lie on a l ...

  8. 把系统时间改到以前后,MyEclipse9.1的工程里的JS文件修改完保存但MyEclipse不会将其不会更新。

    一个任务中,由于本周数据还没有产生,只好把系统时间修改到上周,利用下上周的数据. 修改内容主要是增加查询子句的一个字段,因此,包含SQL的DAO,前台显示的Table和前台操作的JS都需要相应修改,它 ...

  9. 字符串的公共前缀对Mysql B+树查询影响回溯分析

        年前项目组接微信公众号. 上线之后,跟微信相关的用cid列的查询会话的SQL变慢了几十倍!思考这个问题思考了非常久.从出现以来一直是我心头的一个结.cid这一列是建了索引的,普通的cid列更新 ...

  10. SpringMVC上传文件的三种方式(转载)

    直接上代码吧,大伙一看便知 这时:commonsmultipartresolver 的源码,可以研究一下 http://www.verysource.com/code/2337329_1/common ...