PHP單檔案、多個單檔案、多檔案上傳函數的封裝,

來源:互聯網
上載者:User

PHP單檔案、多個單檔案、多檔案上傳函數的封裝,

//表單://s.php//要在選擇上傳檔案時能一次選擇多個檔案,那麼就加multiple="multiple" ,還有注意下name="myFile1"和name="myFile[]"的區別,單檔案、多檔案上傳.<!doctype html><html><head><meta charset="utf-8"><title>無標題文檔</title></head><body><form action="sss.php" method="post" enctype="multipart/form-data">    <input type="file" name="myFile1" /><br/>    <input type="file" name="myFile2" /><br/>    <input type="file" name="myFile[]" /><br/>    <input type="file" name="myFile[]" /><br/>    <input type="file" name="myFile[]" multiple="multiple"/><br/>    <input type="submit" value="上傳檔案"/></form></body></html>//上傳函數的封裝://ss.php<?phpheader('Content-Type:text/html;charset=utf-8');//構建上傳檔案資訊function getFiles(){$i=0;foreach($_FILES as $file){//因為這時$_FILES是個三維數組,並且上傳單檔案或多檔案時,數組的第一維的類型不同,這樣就可以拿來判斷上傳的是單檔案還是多檔案if(is_string($file['name'])){//如果是單檔案$files[$i]=$file;$i++;}elseif(is_array($file['name'])){//如果是多檔案foreach($file['name'] as $key=>$val){$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;}//針對於單檔案、多個單檔案、多檔案的上傳//預設允許上傳的檔案只為圖片類型,並且只有這些圖片類型:$allowExt=array('jpeg','jpg','png','gif');並且檢查上傳的檔案是否為真實的圖片$flag=true//預設上傳儲存的檔案夾為本地的'uploads'檔案夾,允許上傳檔案的大小最大為2Mfunction uploadFile($fileInfo,$path='./uploads',$flag=true,$allowExt=array('jpeg','jpg','png','gif'),$maxSize=2097152){//判斷錯誤號碼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; //如果要不顯示錯誤資訊的話,用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{//匹配錯誤資訊//注意!錯誤資訊沒有5switch($fileInfo['error']){case 1:$res['mes'] = '上傳檔案超過了PHP設定檔中upload_max_filesize選項的值';break;case 2:$res['mes'] = '超過了HTML表單MAX_FILE_SIZE限制的大小';break;case 3:$res['mes'] = '檔案部分被上傳';break;case 4:$res['mes'] = '沒有選擇上傳檔案';break;case 6:$res['mes'] = '沒有找到臨時目錄';break;case 7:$res['mes'] = '檔案寫入失敗';break;case 8:$res['mes'] = '上傳的檔案被PHP擴充程式中斷';break;}return $res;}}?>//common.ss.php<?php//這兩函數也可以一起放到ss.php裡面去//得到副檔名function getExt($filename){return strtolower(pathinfo($filename,PATHINFO_EXTENSION));}//產生唯一字串function getUniName(){return md5(uniqid(microtime(true),true));}?>//上傳後檔案的操作:<?phpheader("content-type:text/html;charset=utf-8");require_once 'ss.php';require_once 'common.ss.php';$files=getFiles();//修改允許上傳檔案的類型,為('jpeg','jpg','png','gif','html','txt'),也可以增加新的,如pdf,pptx等等$allowExt=array('jpeg','jpg','png','gif','html','txt');foreach($files as $fileInfo){//修改上傳儲存的檔案夾為本地的'imooc',如果沒有這個檔案夾,那麼就建立一個//'false'參數:不要檢查上傳的檔案是否為真實的圖片,因為要允許上傳除開圖片類型外的其他類型檔案,如html、txt$res=uploadFile($fileInfo,'imooc',false,$allowExt);echo $res['mes'],'<br/>';$uploadFiles[]=$res['dest'];//如果要不顯示錯誤資訊的話,用@$uploadFiles[]=$res['dest'];}$uploadFiles=array_values(array_filter($uploadFiles));//這樣便於儲存到資料庫print_r($uploadFiles);//列印查看上傳儲存的結果?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.