多文件及单文件上传
来源:互联网 发布:2017格里芬体测数据 编辑:程序博客网 时间:2024/06/09 20:25
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title></title></head><body><form action="doAction3.php" method="post" enctype="multipart/form-data">请选择上传文件:<input type="file" name="myFile[]" /><br/>请选择上传文件:<input type="file" name="myFile[]" /><br/>请选择上传文件:<input type="file" name="myFile[]" /><br/>请选择上传文件:<input type="file" name="myFile1" /><br/>请选择上传文件:<input type="file" name="myFile2" /><br/><input type="submit" value="上传"/></form></body></html>
$fileInfo=uploadFile();print_r($fileInfo);
/** * 生成唯一字符串 * @return string */function getUniName(){return md5(uniqid(microtime(true),true));}/** * 得到文件的扩展名 * @param string $filename * @return string */function getExt($filename){return strtolower(end(explode(".",$filename)));}/** * 构建上传文件信息 * @return array */function buildInfo(){$i=0;foreach($_FILES as $v){//单文件if(is_string($v['name'])){$files[$i]=$v;$i++;}else{//多文件foreach($v['name'] as $key=>$val){$files[$i]['name']=$val;$files[$i]['size']=$v['size'][$key];$files[$i]['tmp_name']=$v['tmp_name'][$key];$files[$i]['error']=$v['error'][$key];$files[$i]['type']=$v['type'][$key];$i++;}}}return $files;}function uploadFile($path="uploads",$allowExt=array("gif","jpeg","png","jpg","wbmp"),$maxSize=2097152,$imgFlag=true){if(!file_exists($path)){mkdir($path,0777,true);}$i=0;$files=buildInfo();foreach($files as $file){if($file['error']===UPLOAD_ERR_OK){$ext=getExt($file['name']);//检测文件的扩展名if(!in_array($ext,$allowExt)){exit("非法文件类型");}//校验是否是一个真正的图片类型if($imgFlag){if(!getimagesize($file['tmp_name'])){exit("不是真正的图片类型");}}//上传文件的大小if($file['size']>$maxSize){exit("上传文件过大");}if(!is_uploaded_file($file['tmp_name'])){exit("不是通过HTTP POST方式上传上来的");}$filename=getUniName().".".$ext;$destination=$path."/".$filename;if(move_uploaded_file($file['tmp_name'],$destination)){$file['name']=$filename;unset($file['error'],$file['tmp_name'],$file['size'],$file['type']);$uploadedFiles[$i]=$file;$i++;}}else{switch($file['error']){case 1:$mes="超过了配置文件上传文件的大小";//UPLOAD_ERR_INI_SIZEbreak;case 2:$mes="超过了表单设置上传文件的大小";//UPLOAD_ERR_FORM_SIZEbreak;case 3:$mes="文件部分被上传";//UPLOAD_ERR_PARTIALbreak;case 4:$mes="没有文件被上传";//UPLOAD_ERR_NO_FILEbreak;case 6:$mes="没有找到临时目录";//UPLOAD_ERR_NO_TMP_DIRbreak;case 7:$mes="文件不可写";//UPLOAD_ERR_CANT_WRITE;break;case 8:$mes="由于PHP的扩展程序中断了文件上传";//UPLOAD_ERR_EXTENSIONbreak;}echo $mes;}}return $uploadedFiles;}
阅读全文
0 0
- 多文件及单文件上传
- 单/多文件上传
- 单多文件上传
- 单文件上传、多文件上传
- 单文件上传和多文件上传
- Yii2文件上传UploadedFile,单文件和多文件上传及验证示例Debug-OK
- 文件上传 单文件 多文件 皆可
- struts2文件上传(单文件/多文件)
- OkhttpUtils单、多文件上传
- MultipartFile实现文件上传(单文件与多文件上传)
- struts2单文件与多文件上传
- 【Struts2】单文件、多文件上传
- Struts1单文件、多文件上传
- Struts2单文件、多文件上传
- PHP单文件/多文件上传
- struts2单文件,多文件上传
- [struts2]单文件与多文件上传
- SpringMVC 单文件和多文件上传
- RecyclerView的简单使用
- HDU1205 吃糖果
- 小米推送常见问题
- 天易47----Springmvc注解方式实现aop配置简单示例
- mysql 向表中插入随机数
- 多文件及单文件上传
- HDU6053 TrickGCD【数学】
- 在eclipse中使用maven创建springMVC项目
- software list
- 单例模式成型版
- Python 3.x 学习:Python 基础代码(二)
- 列表类控件
- @Autowired详解
- TreeJs入门以及基本思想