PHP实现多文件上传操作

  PHP代码部分 文件名:upload_file.php
  1 function moreupload_file(array $files){//接收一个预定义变量$_FILES
  2     $fileArr=array();
  3     if(is_array(current($files)['name']))
  4     {
  5         foreach(current($files)['name'] as $key=>$value)
  6         {
  7             $fileArr[$key]=array();
  8             //$fileArr[$key]+=array('name'=>$value);
  9         }
 10         $keys=['type','tmp_name','error','size','name'];
 11         foreach($keys as $values)
 12         {
 13             foreach(current($files)[$values] as $key=>$value)
 14             {
 15                 
 16                 $fileArr[$key]+=array($values=>$value);
 17             }  
 18         }
 19 
 20     }
 21     $files=$fileArr;
 22     $_FILES=$fileArr;
 23     $file_count=0; 
30
 do{ 31 upload_file(strval(key($files))); 32 if(current($files)['error'] == 0) 33 { 34 ++$file_count; 35 } 36 }while(next($files)) 37 return "共".$file_count."个文件上传成功"; 38 } 39 //单文件上传函数 40 function upload_file(string $file,array $allowext=array('jpeg','png','txt','gif','html','jpg','zip','rar'),int $maxsize=1000000000,string $uploadpath='./upload'){ 41 @define('UPLOAD_ERRS',[ 42 'upload_err_ini_size'=>"上传文件大小超过服务器允许上传的最大值", 43 'upload_err_form_size'=>"上传文件大小超过HTML表单中隐藏域MAX_FILE_SIZE选项指定的值", 44 'upload_err_partial'=>"文件只有部分被上传", 45 'no_upload_file_select'=>"没有选择文件上传", 46 'temp_file_none'=>"服务器临时文件夹丢失", 47 'upload_err_no_tmp_dir'=>"服务器临时文件夹丢失", 48 'upload_err_cant_write'=>"文件写入失败", 49 'upload_err_extension'=>"php上传扩展没有打开", 50 'no_allow_ext'=>"不允许该类文件上传", 51 'upload_err_file_max'=>"超过文件上传大小", 52 'no_http_post'=>"没有使用http协议post方式上传", 53 'move_err_file'=>"文件移动失败" 54 55 56 ]); 57 58 $fileinfo=$_FILES[$file]; 59 60 if($fileinfo['error']===UPLOAD_ERR_OK) 61 { 62 63 $ext=strtolower(pathinfo($fileinfo['name'],PATHINFO_EXTENSION)); 64 if(!in_array($ext,$allowext)) 65 { 66 return UPLOAD_ERRS['no_allow_ext']; 67 } 68 if($fileinfo['size']>$maxsize) 69 { 70 return UPLOAD_ERRS['upload_err_file_max']; 71 } 72 if(!is_uploaded_file($fileinfo['tmp_name'])) 73 { 74 return UPLOAD_ERRS['no_http_post']; 75 } 76 if(!is_dir($uploadpath)) 77 { 78 79 mkdir($uploadpath,0777,true); 80 } 81 $dest=$uploadpath."/".md5(time().str_shuffle('abcdefghijklmnopquvwxyz1234567890')).".".$ext; 82 if(@!move_uploaded_file($fileinfo['tmp_name'],$dest)) 83 { 84 return UPLOAD_ERRS['move_err_file']; 85 } 86 return "文件上传成功,文件路径:".$dest.'<br>'; 87 } 88 else{ 89 90 switch($fileinfo['error']) 91 { 92 case 1: $msg=UPLOAD_ERRS['upload_err_ini_size']; 93 break; 94 case 2: $msg=UPLOAD_ERRS['upload_err_form_size']; 95 break; 96 case 3: $msg=UPLOAD_ERRS['upload_err_partial']; 97 break; 98 case 4: $msg=UPLOAD_ERRS['no_upload_file_select']; 99 break; 100 case 5: $msg=UPLOAD_ERRS['temp_file_none']; 101 break; 102 case 6: $msg=UPLOAD_ERRS['upload_err_no_tmp_dir']; 103 break; 104 case 7: $msg=UPLOAD_ERRS['upload_err_cant_write']; 105 break; 106 case 8: $msg=UPLOAD_ERRS['upload_err_extension']; 107 break; 108 } 109 return $msg; 110 } 111 112 }
  moreupload_file($_FILES);//调用上面定义的函数

  

  HTML代码部分

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <meta name="viewport" content="width=device-width, initial-scale=1.0">
 6     <meta http-equiv="X-UA-Compatible" content="ie=edge">
 7     <title>Document</title>
 8 </head>
 9 <body>
10     <div id="box"></div>
11     
12     <form action="./upload_file.php" method="post" enctype="multipart/form-data" id="formdata">
13         <input type="file" name="file[]" id="file" multiple="multiplt">

14 <input type="submit" value="文件上传" id="uploadFile">
15     </form>
16     <script type="text/javascript"  src="https://code.jquery.com/jquery-3.4.1.min.js"></script>
17 <script>
18     
19     var fileArr=document.getElementById('file').files;
20     var fileOne=document.getElementById('file');
21     var formOne=null;
22     fileOne.onchange=function(){
23         var fileArr=document.getElementById('file').files;
24         var box=document.getElementById('box');
25         var file;
26         for(file in fileArr)
27         {
28             if(fileArr[file]['name'] && !isNaN(file))
29             {
30             box.innerHTML+='<img src="'+window.URL.createObjectURL(fileArr[file])+'" style="width:200px;">';
31             
32             }
33         }
34        
35      
36     }
37     
38 
39 </script>
40 </body>
41 </html>
HTML部分效果图:




moreupload_file()函数接收到的$_FILES 数据格式



多个文件上传的数据和单个文件上传进行对比一下。



这一对比就可以看出他们之间的关系,相比单文件接收到的数据,多文件用数组把相同键名的数据存储到了一个数组当中,
再看一下单文件上传函数upload_file()接收到的参数是$_FILES[]的键值,
所以需要对多文件接收到的参数进行处理
moreupload_file()中的代码段
 1 foreach(current($files)['name'] as $key=>$value)
 2         {
 3             $fileArr[$key]=array();//定义一个二维数组$fileArr
 4             //$fileArr[$key]+=array('name'=>$value);
 5         }
 6
 7         $keys=['type','tmp_name','error','size','name'];
 8         foreach($keys as $values)
 9         {
10             foreach(current($files)[$values] as $key=>$value)
11             {
12                 $fileArr[$key]+=array($values=>$value);//将数据处理存入刚才定义的二维数组$fileArr
13             }  
14         }
这个就是上面定义的二维数组$fileArr
这种结构就和我们刚才的单文件上传接收到的数据结构相同了
接下来就只要循环调用单文件上传函数upload_file()就可以了

 

 

 

 

posted @ 2020-03-17 11:57  yav  阅读(1027)  评论(0)    收藏  举报