多文件的上傳實現
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>
這里的思路,從print_r($_FILES)中去找,打印出來看到是個二維數組,很簡單,遍歷去用就好了!
- <?php
- //print_r($_FILES);
- header('content-type:text/html;charset=utf-8');
- include_once 'upFunc.php';
- foreach ($_FILES as $fileInfo){
- $file[]=uploadFile($fileInfo);
- }
上面那個function的定義改一下,給定一些默認值
- function uploadFile($fileInfo,$path="uploads",$allowExt=array('jpeg','jpg','png','tif'),$maxSize=10485760){
這樣子,簡單是簡單,但遇到一些問題。
正常的上傳4個圖片是沒問題,但要是中間激活了函數中的exit,就會立即停止,導致其他圖片也無法上傳。
2 升級版封裝
旨在實現針對多個或單個文件上傳的封裝
首先這樣子寫個靜態文件
打印查看一下$_FILES數組內容
- <!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>
- 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
- )
- )
- )
可以得到一個三維數組。
復雜是復雜了,但復雜的有規律,各項數值都在一起了,很方便我們取值!!
所以先得到文件信息,變成單文件處理那種信息
然后之前的那種exit錯誤,就把exit改一下就好了,這里用res
- 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;
- }
里面封裝了兩個小的
- 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;
- }
- }
然后靜態中,用multiple屬性實現多個文件的輸入;
- function getExt($filename){
- return strtolower(pathinfo($filename,PATHINFO_EXTENSION));
- }
- /**
- * 產生唯一字符串
- * @return string
- */
- function getUniName(){
- return md5(uniqid(microtime(true),true));
- }
- <!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);
新聞熱點
疑難解答