PHP如何?多檔案上傳

來源:互聯網
上載者:User

本文主要和大家分享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. 實現檔案上傳的關鍵一步在於在臨時檔案消失前,需要將臨時檔案儲存到Web伺服器或檔案伺服器。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['myPicture'];    $file =array();    for ($i=0; $i < count($arr['name']); $i++) {         $file[$i]['name'] = $arr['name'][$i];        $file[$i]['type'] = $arr['type'][$i];        $file[$i]['tmp_name'] = $arr['tmp_name'][$i];        $file[$i]['error'] = $arr['error'][$i];        $file[$i]['size'] = $arr['size'][$i];    }    for ($i=0; $i < count($file); $i++) {         switch ($file[$i]['error']) {            case 0:                          $fileName = $file[$i]['name'];                $fileTemp = $file[$i]['tmp_name'];                $destination = "uploads/" . $file[$i]['name'];                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. 單擊提交按鈕,瀏覽器使用者將包含上傳檔案的表單資料提交給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)  '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['myPicture'];    $file =array();    for ($i=0; $i < count($arr['name']); $i++) {         $file[$i]['name'] = $arr['name'][$i];        $file[$i]['type'] = $arr['type'][$i];        $file[$i]['tmp_name'] = $arr['tmp_name'][$i];        $file[$i]['error'] = $arr['error'][$i];        $file[$i]['size'] = $arr['size'][$i];    }    for ($i=0; $i < count($file); $i++) {         switch ($file[$i]['error']) {            case 0:                          $fileName = $file[$i]['name'];                $fileTemp = $file[$i]['tmp_name'];                $destination = "uploads/" . $file[$i]['name'];                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;        }     }   ?>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.