您好,欢迎访问一九零五行业门户网

如何实现php多文件上传封装

多文件的上传实现
1 利用单文件封装
<!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>insert title here</title> </head> <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="myfile3" /><br/> 请选择您要上传的文件:<input type="file" name="myfile4" /><br/> <input type="submit" value="上传"/> </form> </body> </html>
<?php //print_r($_files); header('content-type:text/html;charset=utf-8'); include_once 'upfunc.php'; foreach ($_files as $fileinfo){ $file[]=uploadfile($fileinfo); }
这里的思路,从print_r($_files)中去找,打印出来看到是个二维数组,很简单,遍历去用就好了!
上面那个function的定义改一下,给定一些默认值
function uploadfile($fileinfo,$path="uploads",$allowext=array('jpeg','jpg','png','tif'),$maxsize=10485760){
这样子,简单是简单,但遇到一些问题。
正常的上传4个图片是没问题,但要是中间激活了函数中的exit,就会立即停止,导致其他图片也无法上传。
2 升级版封装
旨在实现针对多个或单个文件上传的封装
首先这样子写个静态文件
<!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>insert title here</title> </head> <body> <form action="doaction5.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="myfile[]" /><br/> <input type="submit" value="上传"/> </form> </body> </html>
打印一下$_files
array ( [myfile] => array ( [name] => array ( [0] => test32.png [1] => test32.png [2] => 333.png [3] => test41.png ) [type] => array ( [0] => image/png [1] => image/png [2] => image/png [3] => image/png ) [tmp_name] => array ( [0] => d:\wamp\tmp\php831c.tmp [1] => d:\wamp\tmp\php834c.tmp [2] => d:\wamp\tmp\php837c.tmp [3] => d:\wamp\tmp\php83bb.tmp ) [error] => array ( [0] => 0 [1] => 0 [2] => 0 [3] => 0 ) [size] => array ( [0] => 46174 [1] => 46174 [2] => 34196 [3] => 38514 ) ) )
可以得到一个三维数组。
复杂是复杂了,但复杂的有规律,各项数值都在一起了,很方便我们取值!!
所以先得到文件信息,变成单文件处理那种信息
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){ //我的天,这个$key用的diao $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; }
然后之前的那种exit错误,就把exit改一下就好了,这里用res
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 //判断错误号 $res=array(); 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; } }
里面封装了两个小的
function getext($filename){ return strtolower(pathinfo($filename,pathinfo_extension)); } /** * 产生唯一字符串 * @return string */ function getuniname(){ return md5(uniqid(microtime(true),true)); }
然后静态中,用multiple属性实现多个文件的输入;
<!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>insert title here</title> </head> <body> <form action="doaction6.php" method="post" enctype="multipart/form-data"> 请选择您要上传的文件:<input type="file" name="myfile[]" multiple='multiple' /><br/> <input type="submit" value="上传"/> </form> </body> </html>
<?php //print_r($_files); header("content-type:text/html;charset=utf-8"); require_once 'upfunc2.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);
这样子的几个文件,就实现比较强大的面向过程的上传文件的功能。
以上就是如何实现php多文件上传封装的详细内容。
其它类似信息

推荐信息