亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

將文件的上傳下載操作封裝成一個單獨的方法

Original 2019-04-21 14:50:17 238
abstrakt:<?php  //文件上傳操作  function upload_file($fileInfo,$uploadPath="./upload",$allowExt=['txt','png','jpg','jpeg','gif','html','
<?php
 //文件上傳操作
 function upload_file($fileInfo,$uploadPath="./upload",$allowExt=['txt','png','jpg','jpeg','gif','html','php'],$maxSize=1000000)
 {
     if($fileInfo['error']===0){
         //獲取文件后綴
         $ext=strtolower(pathinfo($fileInfo['name'],PATHINFO_EXTENSION));
         //判斷文件類型
         if(!in_array($ext,$allowExt)){
             return '非法文件類型!';
         }
         //判斷文件大小
         if($fileInfo['size']>$maxSize){
             return '超出文件上傳最大值';
         }
         //判斷文件上傳方式
         if(!is_uploaded_file($fileInfo['tmp_name'])){
             return '非法上傳文件!';
         }
         //判斷需要移動到的目錄是否存在
         if(!is_dir($uploadPath)){
             mkdir($uploadPath,0777,true);
         }
         //生成唯一的文件名:uniqid生成唯一的id,microtime 返回當前unix時間戳中的微秒
         $uniName=md5(uniqid(microtime(true),true)).".".$ext;
         //拼接路徑以及文件名
         $dest=$uploadPath."/".$uniName;
         //將文件移動到指定路徑
         if(!move_uploaded_file($fileInfo['tmp_name'],$dest)){
             return '文件上傳失?。?#39;;
         }else{
             return '文件上傳成功!';
         }
     }else{
         switch ($fileInfo['error']){
             case 1:
                 $res='上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值!';
                 break;
             case 2:
                 $res='上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值!';
                 break;
             case 3:
                 $res='文件只有部分被上傳!';
                 break;
             case 4:
                 $res='沒有文件被上傳!';
                 break;
             case 6:
                 $res='找不到臨時文件夾';
                 break;
             case 7:
                 $res='文件寫入失敗';
                 break;
         }
     }
     return $res;
 }
 
 //文件下載操作
 
 function dow_file($fileName){
     //告訴瀏覽器返回文件的大小
     header('Accept-Length:'.filesize($fileName));
     //告訴瀏覽器文件作為附件處理,并告訴瀏覽器文件下載完的文件名
     header('Content-Disposition:attachment;fileName='.basename($fileName));
     //輸出文件
     readfile($fileName);
 }


Korrigierender Lehrer:西門大官人Korrekturzeit:2019-04-22 10:28:36
Zusammenfassung des Lehrers:在企業(yè)開發(fā)中,函數(shù)的返回值一般是不直接返回中文字符的。

Versionshinweise

Beliebte Eintr?ge