首頁  >  文章  >  後端開發  >  PHP如何實作多檔案上傳

PHP如何實作多檔案上傳

小云云
小云云原創
2018-03-30 13:12:475167瀏覽

本文主要跟大家分享PHP如何實現多檔案上傳,希望能幫助大家。

PHP檔案上傳流程
1. 點擊提交按鈕,瀏覽器使用者將包含上傳檔案的表單資料提交給PHP處理程序
2. Web伺服器和PHP預處理器首先判斷表單資料的大小是否超過php.ini設定檔中的post_max_size選項設定的上限值。
若超過,PHP處理程序將無法得到任何表單數據,此時不僅上傳文件失敗,而且表單控制項中填寫的數據也會提交失敗,也就是說:PHP處理程序預定義變數$_GET、$_POST、$_FILES將為空數組。
   若沒有超過,檔案上傳進入第3步驟檢定。
3. 檢驗表單中的檔案大小是否超過表單隱藏域MAX_FILE_SIZE#設定的上限值。
   若超過,PHP預處理器返回狀態碼2,檔案上傳失敗。
   若沒有超過,檔案上傳進入第4步驟檢定。
(當有多個檔案進行上傳時,某個檔案上傳框導致的檔案上傳失敗,不會影響其他檔案上傳框的上傳結果)
4. 檢驗表單中的檔案是否超過php.ini設定檔中upload_max_filesize選項設定的上限值。
   若超過,PHP預處理器返回狀態碼1,檔案上傳失敗。
   若沒有超過,檔案上傳進入第5步檢定。
5. PHP實作上傳檔案需要在php.ini設定檔upload_tmp_dir選項定義的目錄中建立一個與上傳檔案一一對應的暫存檔案(預設拓展名為tmp),上傳成功後,暫存檔案立即消失,此時PHP預處理器的回傳狀態碼0。
   但是有時由於默寫原因(如max_execution_time選項設定過小或網速慢等原因),上傳部分文件後不再繼續上傳剩余文件,導致文件上傳失敗,此時PHP預處理器返回狀態碼3
   若通過,文件上傳進入第6步檢定。
6. 實現檔案上傳的關鍵步驟在於在暫存檔案消失前,需要將暫存檔案儲存到網路伺服器或檔案伺服器。 PHP提供的兩個函數:is_uploaded_file()函數和move_uploaded_file()函數,可以幫助完成這一步的工作


多個檔案上傳要注意的就是相同的name所儲存的檔案內容是按照下面的形式放在陣列中的。是五個數組,依照檔案的五個參數分別存放的,並非三個數組。所以如果直接使用count($_FILES[‘$myPicture’]),答案是5。

array (size=5)  'name' => 
    array (size=3)      0 => string '1.txt' (length=5)      1 => string '2.txt' (length=5)      2 => string '3.txt' (length=5)  'type' => 
    array (size=3)      0 => string 'text/plain' (length=10)      1 => string 'text/plain' (length=10)      2 => string 'text/plain' (length=10)  'tmp_name' => 
    array (size=3)      0 => string 'D:\wamp64\tmp\phpC5E8.tmp' (length=25)      1 => string 'D:\wamp64\tmp\phpC5E9.tmp' (length=25)      2 => string 'D:\wamp64\tmp\phpC5EA.tmp' (length=25)  'error' => 
    array (size=3)      0 => int 0
      1 => int 0
      2 => int 0
  'size' => 
    array (size=3)      0 => int 0
      1 => int 0
      2 => int 0

index.php檔案

<form action="fileSystem.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    <input type="file" name="myPicture[]" size= "25" maxlength="100"><br>
    <input type="file" name="myPicture[]" size= "25" maxlength="100"><br>
    <input type="file" name="myPicture[]" size= "25" maxlength="100"><br>
    <input type="submit" value="提交">
</form>

fileSystem檔案

<?php
    if (empty($_POST)) {        exit("提交的表单数据超过post_max_size的配置");
    }    $arr = $_FILES[&#39;myPicture&#39;];    $file =array();    for ($i=0; $i < count($arr[&#39;name&#39;]); $i++) { 
        $file[$i][&#39;name&#39;] = $arr[&#39;name&#39;][$i];        $file[$i][&#39;type&#39;] = $arr[&#39;type&#39;][$i];        $file[$i][&#39;tmp_name&#39;] = $arr[&#39;tmp_name&#39;][$i];        $file[$i][&#39;error&#39;] = $arr[&#39;error&#39;][$i];        $file[$i][&#39;size&#39;] = $arr[&#39;size&#39;][$i];
    }    for ($i=0; $i < count($file); $i++) { 
        switch ($file[$i][&#39;error&#39;]) {            case 0:          
                $fileName = $file[$i][&#39;name&#39;];                $fileTemp = $file[$i][&#39;tmp_name&#39;];                $destination = "uploads/" . $file[$i][&#39;name&#39;];
                move_uploaded_file($fileTemp, $destination);                echo "上传成功";                break;            case 1:                echo "上传附件超过php.ini中的upload_max_filesize选项的限制";                break;            case 2:                echo "上传附件的大小超过了form表单MAX_FILE_SIZE选项指定的值";                break;            case 3:                echo "附件只有部分被上传";                break;            case 4:                echo "没有选择上传附件";                break;
        } 
    }   
?>

               

PHP檔案上傳流程##1.1.提交按鈕,瀏覽器使用者將包含上傳檔案的表單資料提交給PHP處理程序
2. Web伺服器和PHP預處理器首先判斷表單資料的大小是否超過php.ini設定檔中的
post_max_size選項設定的上限值。 若超過,PHP處理程序將無法得到任何表單數據,此時不僅上傳文件失敗,而且表單控制項中填寫的數據也會提交失敗,也就是說:PHP處理程序預定義變數$_GET、$_POST、$_FILES將為空數組。
   若沒有超過,檔案上傳進入第3步驟檢定。
3. 檢驗表單中的檔案大小是否超過表單隱藏域
MAX_FILE_SIZE#設定的上限值。    若超過,PHP預處理器返回狀態碼2,檔案上傳失敗。
   若沒有超過,檔案上傳進入第4步驟檢定。
(當有多個檔案進行上傳時,某個檔案上傳框導致的檔案上傳失敗,不會影響其他檔案上傳框的上傳結果)
4. 檢驗表單中的檔案是否超過php.ini設定檔中
upload_max_filesize選項設定的上限值。    若超過,PHP預處理器返回狀態碼1,檔案上傳失敗。
   若沒有超過,檔案上傳進入第5步檢定。
5. PHP實作上傳檔案需要在php.ini設定檔upload_tmp_dir選項定義的目錄中建立一個與上傳檔案一一對應的暫存檔案(預設拓展名為tmp),上傳成功後,暫存檔案立即消失,此時PHP預處理器的回傳狀態碼0。
   但是有時由於默寫原因(如max_execution_time選項設定過小或網速慢等原因),上傳部分文件後不再繼續上傳剩余文件,導致文件上傳失敗,此時PHP預處理器返回狀態碼3
   若通過,文件上傳進入第6步檢定。
6. 實作檔案上傳的關鍵步驟在於在暫存檔案消失前,需要將暫存檔案儲存到Web伺服器或檔案伺服器。 PHP提供的兩個函數:
is_uploaded_file()函數和move_uploaded_file()函數,可以幫助完成這一步的工作


多個檔案上傳要注意的就是相同的name所儲存的檔案內容是按照下面的形式放在陣列中的。是五個數組,依照檔案的五個參數分別存放的,並非三個數組。所以如果直接使用count($_FILES[‘$myPicture’]),答案是5。

array (size=5)  &#39;name&#39; => 
    array (size=3)      0 => string &#39;1.txt&#39; (length=5)      1 => string &#39;2.txt&#39; (length=5)      2 => string &#39;3.txt&#39; (length=5)  &#39;type&#39; => 
    array (size=3)      0 => string &#39;text/plain&#39; (length=10)      1 => string &#39;text/plain&#39; (length=10)      2 => string &#39;text/plain&#39; (length=10)  &#39;tmp_name&#39; => 
    array (size=3)      0 => string &#39;D:\wamp64\tmp\phpC5E8.tmp&#39; (length=25)      1 => string &#39;D:\wamp64\tmp\phpC5E9.tmp&#39; (length=25)      2 => string &#39;D:\wamp64\tmp\phpC5EA.tmp&#39; (length=25)  &#39;error&#39; => 
    array (size=3)      0 => int 0
      1 => int 0
      2 => int 0
  &#39;size&#39; => 
    array (size=3)      0 => int 0
      1 => int 0
      2 => int 0

index.php檔案

<form action="fileSystem.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    <input type="file" name="myPicture[]" size= "25" maxlength="100"><br>
    <input type="file" name="myPicture[]" size= "25" maxlength="100"><br>
    <input type="file" name="myPicture[]" size= "25" maxlength="100"><br>
    <input type="submit" value="提交">
</form>

fileSystem檔案

<?php
    if (empty($_POST)) {        exit("提交的表单数据超过post_max_size的配置");
    }    $arr = $_FILES[&#39;myPicture&#39;];    $file =array();    for ($i=0; $i < count($arr[&#39;name&#39;]); $i++) { 
        $file[$i][&#39;name&#39;] = $arr[&#39;name&#39;][$i];        $file[$i][&#39;type&#39;] = $arr[&#39;type&#39;][$i];        $file[$i][&#39;tmp_name&#39;] = $arr[&#39;tmp_name&#39;][$i];        $file[$i][&#39;error&#39;] = $arr[&#39;error&#39;][$i];        $file[$i][&#39;size&#39;] = $arr[&#39;size&#39;][$i];
    }    for ($i=0; $i < count($file); $i++) { 
        switch ($file[$i][&#39;error&#39;]) {            case 0:          
                $fileName = $file[$i][&#39;name&#39;];                $fileTemp = $file[$i][&#39;tmp_name&#39;];                $destination = "uploads/" . $file[$i][&#39;name&#39;];
                move_uploaded_file($fileTemp, $destination);                echo "上传成功";                break;            case 1:                echo "上传附件超过php.ini中的upload_max_filesize选项的限制";                break;            case 2:                echo "上传附件的大小超过了form表单MAX_FILE_SIZE选项指定的值";                break;            case 3:                echo "附件只有部分被上传";                break;            case 4:                echo "没有选择上传附件";                break;
        } 
    }   
?>

相關推薦:

php3.2實作多檔案上傳

一種PHP實作多檔案上傳的方法實例解析

#實例分析PHP單一檔案與多檔案上傳

#

以上是PHP如何實作多檔案上傳的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn