php多文件上傳封裝_PHP教程

      編輯Tag賺U幣
      教程Tag:php上傳添加

      推薦:php生成圓角圖片的方法
      具體如下: 代碼如下:?php $image_file = $_GET['src']; $corner_radius = isset($_GET['radius']) ? $_GET['radius'] : 20; // The default corner radius is set to 20px $topleft = (isset($_GET['topleft']) and $_GET['topleft'] == no) ? false : true; // Top-l

      多文件的上傳實(shí)現(xiàn)

      1 利用單文件封裝

      1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
      2. <html> 
      3. <head> 
      4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
      5. <title>Insert title here</title> 
      6. </head> 
      7. <body> 
      8. <form action="doAction5.php" method="post" enctype="multipart/form-data"> 
      9. 請選擇您要上傳的文件:<input type="file" name="myFile1" /><br/> 
      10. 請選擇您要上傳的文件:<input type="file" name="myFile2" /><br/> 
      11. 請選擇您要上傳的文件:<input type="file" name="myFile3" /><br/> 
      12. 請選擇您要上傳的文件:<input type="file" name="myFile4" /><br/> 
      13. <input type="submit" value="上傳"/> 
      14. </form> 
      15. </body> 
      16. </html> 
      1. <?php 
      2. //print_r($_FILES); 
      3. header('content-type:text/html;charset=utf-8'); 
      4. include_once 'upFunc.php'
      5. foreach ($_FILES as $fileInfo){ 
      6.     $file[]=uploadFile($fileInfo); 
      這里的思路,從print_r($_FILES)中去找,打印出來看到是個(gè)二維數(shù)組,很簡單,遍歷去用就好了!

      上面那個(gè)function的定義改一下,給定一些默認(rèn)值

      1. function uploadFile($fileInfo,$path="uploads",$allowExt=array('jpeg','jpg','png','tif'),$maxSize=10485760){ 

      這樣子,簡單是簡單,但遇到一些問題。

      正常的上傳4個(gè)圖片是沒問題,但要是中間激活了函數(shù)中的exit,就會(huì)立即停止,導(dǎo)致其他圖片也無法上傳。

      2 升級版封裝

      旨在實(shí)現(xiàn)針對多個(gè)或單個(gè)文件上傳的封裝

      首先這樣子寫個(gè)靜態(tài)文件

      1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
      2. <html> 
      3. <head> 
      4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
      5. <title>Insert title here</title> 
      6. </head> 
      7. <body> 
      8. <form action="doAction5.php" method="post" enctype="multipart/form-data"> 
      9. 請選擇您要上傳的文件:<input type="file" name="myFile[]" /><br/> 
      10. 請選擇您要上傳的文件:<input type="file" name="myFile[]" /><br/> 
      11. 請選擇您要上傳的文件:<input type="file" name="myFile[]" /><br/> 
      12. 請選擇您要上傳的文件:<input type="file" name="myFile[]" /><br/> 
      13. <input type="submit" value="上傳"/> 
      14. </form> 
      15. </body> 
      16. </html> 
      打印查看一下$_FILES數(shù)組內(nèi)容
      1. Array 
      2.     [myFile] => Array 
      3.         ( 
      4.             [name] => Array 
      5.                 ( 
      6.                     [0] => test32.png 
      7.                     [1] => test32.png 
      8.                     [2] => 333.png 
      9.                     [3] => test41.png 
      10.                 ) 
      11.             [type] => Array 
      12.                 ( 
      13.                     [0] => image/png 
      14.                     [1] => image/png 
      15.                     [2] => image/png 
      16.                     [3] => image/png 
      17.                 ) 
      18.             [tmp_name] => Array 
      19.                 ( 
      20.                     [0] => D:\wamp\tmp\php831C.tmp 
      21.                     [1] => D:\wamp\tmp\php834C.tmp 
      22.                     [2] => D:\wamp\tmp\php837C.tmp 
      23.                     [3] => D:\wamp\tmp\php83BB.tmp 
      24.                 ) 
      25.             [error] => Array 
      26.                 ( 
      27.                     [0] => 0 
      28.                     [1] => 0 
      29.                     [2] => 0 
      30.                     [3] => 0 
      31.                 ) 
      32.             [size] => Array 
      33.                 ( 
      34.                     [0] => 46174 
      35.                     [1] => 46174 
      36.                     [2] => 34196 
      37.                     [3] => 38514 
      38.                 ) 
      39.         ) 

      可以得到一個(gè)三維數(shù)組。

      復(fù)雜是復(fù)雜了,但復(fù)雜的有規(guī)律,各項(xiàng)數(shù)值都在一起了,很方便我們?nèi)≈担。?/p>

      所以先得到文件信息,變成單文件處理那種信息

      1. function getFiles(){ 
      2.     $i=0; 
      3.     foreach($_FILES as $file){ 
      4.         if(is_string($file['name'])){  //單文件判定 
      5.             $files[$i]=$file
      6.             $i++; 
      7.         }elseif(is_array($file['name'])){ 
      8.             foreach($file['name'as $key=>$val){  //我的天,這個(gè)$key用的diao 
      9.                 $files[$i]['name']=$file['name'][$key]; 
      10.                 $files[$i]['type']=$file['type'][$key]; 
      11.                 $files[$i]['tmp_name']=$file['tmp_name'][$key]; 
      12.                 $files[$i]['error']=$file['error'][$key]; 
      13.                 $files[$i]['size']=$file['size'][$key]; 
      14.                 $i++; 
      15.             } 
      16.         } 
      17.     } 
      18.     return $files
      19.       
      然后之前的那種exit錯(cuò)誤,就把exit改一下就好了,這里用res
      1. function uploadFile($fileInfo,$path='./uploads',$flag=true,$maxSize=1048576,$allowExt=array('jpeg','jpg','png','gif')){ 
      2.     //$flag=true; 
      3.     //$allowExt=array('jpeg','jpg','gif','png'); 
      4.     //$maxSize=1048576;//1M 
      5.     //判斷錯(cuò)誤號(hào) 
      6.     $res=array(); 
      7.     if($fileInfo['error']===UPLOAD_ERR_OK){ 
      8.         //檢測上傳得到小 
      9.         if($fileInfo['size']>$maxSize){ 
      10.             $res['mes']=$fileInfo['name'].'上傳文件過大'
      11.         } 
      12.         $ext=getExt($fileInfo['name']); 
      13.         //檢測上傳文件的文件類型 
      14.         if(!in_array($ext,$allowExt)){ 
      15.             $res['mes']=$fileInfo['name'].'非法文件類型'
      16.         } 
      17.         //檢測是否是真實(shí)的圖片類型 
      18.         if($flag){ 
      19.             if(!getimagesize($fileInfo['tmp_name'])){ 
      20.                 $res['mes']=$fileInfo['name'].'不是真實(shí)圖片類型'
      21.             } 
      22.         } 
      23.         //檢測文件是否是通過HTTP POST上傳上來的 
      24.         if(!is_uploaded_file($fileInfo['tmp_name'])){ 
      25.             $res['mes']=$fileInfo['name'].'文件不是通過HTTP POST方式上傳上來的'
      26.         } 
      27.         if($resreturn $res
      28.         //$path='./uploads'; 
      29.         if(!file_exists($path)){ 
      30.             mkdir($path,0777,true); 
      31.             chmod($path,0777); 
      32.         } 
      33.         $uniName=getUniName(); 
      34.         $destination=$path.'/'.$uniName.'.'.$ext
      35.         if(!move_uploaded_file($fileInfo['tmp_name'],$destination)){ 
      36.             $res['mes']=$fileInfo['name'].'文件移動(dòng)失敗'
      37.         } 
      38.         $res['mes']=$fileInfo['name'].'上傳成功'
      39.         $res['dest']=$destination
      40.         return $res
      41.           
      42.     }else
      43.         //匹配錯(cuò)誤信息 
      44.         switch ($fileInfo ['error']) { 
      45.             case 1 : 
      46.                 $res['mes'] = '上傳文件超過了PHP配置文件中upload_max_filesize選項(xiàng)的值'
      47.                 break
      48.             case 2 : 
      49.                 $res['mes'] = '超過了表單MAX_FILE_SIZE限制的大小'
      50.                 break
      51.             case 3 : 
      52.                 $res['mes'] = '文件部分被上傳'
      53.                 break
      54.             case 4 : 
      55.                 $res['mes'] = '沒有選擇上傳文件'
      56.                 break
      57.             case 6 : 
      58.                 $res['mes'] = '沒有找到臨時(shí)目錄'
      59.                 break
      60.             case 7 : 
      61.             case 8 : 
      62.                 $res['mes'] = '系統(tǒng)錯(cuò)誤'
      63.                 break
      64.         } 
      65.         return $res
      66.     } 
      里面封裝了兩個(gè)小的
      1. function getExt($filename){ 
      2.     return strtolower(pathinfo($filename,PATHINFO_EXTENSION)); 
      3. /** 
      4.  * 產(chǎn)生唯一字符串 
      5.  * @return string 
      6.  */ 
      7. function getUniName(){ 
      8.     return md5(uniqid(microtime(true),true)); 
      然后靜態(tài)中,用multiple屬性實(shí)現(xiàn)多個(gè)文件的輸入;
      1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
      2. <html> 
      3. <head> 
      4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
      5. <title>Insert title here</title> 
      6. </head> 
      7. <body> 
      8. <form action="doAction6.php" method="POST" enctype="multipart/form-data"> 
      9. 請選擇您要上傳的文件:<input type="file" name="myFile[]" multiple='multiple' /><br/> 
      10. <input type="submit" value="上傳"/> 
      11. </form> 
      12. </body> 
      13. </html> 
      1. <?php 
      2. //print_r($_FILES); 
      3. header("content-type:text/html;charset=utf-8"); 
      4. require_once 'upFunc2.php'
      5. require_once 'common.func.php'
      6. $files=getFiles(); 
      7. // print_r($files); 
      8. foreach($files as $fileInfo){ 
      9.     $res=uploadFile($fileInfo); 
      10.     echo $res['mes'],'<br/>'
      11.     $uploadFiles[]=@$res['dest']; 
      12. $uploadFiles=array_values(array_filter($uploadFiles)); 
      13. //print_r($uploadFiles); 

      通過以上的幾個(gè)文件,就能實(shí)現(xiàn)比較強(qiáng)大的面向過程的上傳文件的功能了。

      分享:php按單詞截取字符串的方法
      這里指定字符串和單詞數(shù)量進(jìn)行截取 代碼如下:?php function limit_words($string, $word_limit) { $words = explode( ,$string); return implode( ,array_splice($words,0,$word_limit)); } //Example Usage $content = Lorem ipsum dolor sit amet, consectetur adipi

      來源:模板無憂//所屬分類:PHP教程/更新時(shí)間:2017-06-27
      相關(guān)PHP教程