Home > Backend Development > PHP Tutorial > How to implement multiple file uploads in PHP

How to implement multiple file uploads in PHP

小云云
Release: 2023-03-22 18:42:01
Original
5253 people have browsed it

This article mainly shares with you how to implement multiple file uploads in PHP. I hope it can help you.

PHP file upload process
1. Click the submit button, and the browser user submits the form data containing the uploaded file to the PHP handler
2. The web server and PHP preprocessor first determine the form Whether the size of the data exceeds the upper limit set by the post_max_size option in the php.ini configuration file.
If it exceeds, the PHP handler will not be able to obtain any form data. At this time, not only the file upload will fail, but the data filled in the form control will also fail to be submitted, that is to say: the PHP handler predefined variables $_GET, $_POST, $_FILES will be an empty array.
If it does not exceed the limit, the file will be uploaded to step 3 for verification.
3. Check whether the file size in the form exceeds the upper limit set by the form hidden field MAX_FILE_SIZE.
If it exceeds, the PHP preprocessor returns status code 2 and the file upload fails.
If it does not exceed the limit, the file will be uploaded to step 4 for verification.
(When there are multiple files to upload, the file upload failure caused by a certain file upload box will not affect the upload results of other file upload boxes)
4. Check whether the files in the form exceed the php.ini configuration file upload_max_filesizeThe upper limit value set by the option.
If it exceeds, the PHP preprocessor returns status code 1 and the file upload fails.
If it does not exceed the limit, the file will be uploaded to step 5 for verification.
5. To implement file upload in PHP, you need to create a temporary file corresponding to the uploaded file one-to-one (the default extension is tmp) in the directory defined by the upload_tmp_dir option in the php.ini configuration file. After the upload is successful, the temporary file disappears immediately. At this time, the PHP preprocessor returns status code 0.
But sometimes due to tacit writing reasons (such as the max_execution_time option setting is too small or the network speed is slow, etc.), after uploading some files, the remaining files are no longer uploaded, causing the file upload to fail. At this time, the PHP preprocessor returns status code 3
If passed, the file will be uploaded to step 6 for inspection.
6. The key step to achieve file upload is to save the temporary file to the web server or file server before the temporary file disappears. The two functions provided by PHP: is_uploaded_file() function and move_uploaded_file() function can help complete this step.


Multiple file upload requirements Note that the contents of files saved with the same name are placed in the array in the following form. They are five arrays, stored separately according to the five parameters of the file, not three arrays. So if you use count($_FILES[‘$myPicture’]) directly, the answer is 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
Copy after login
Copy after login

index.php file

<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>
Copy after login
Copy after login

fileSystem file

<?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;
        } 
    }   
?>
Copy after login
Copy after login

           

PHP file upload process
1. Click Submit button, the browser user submits the form data containing the uploaded file to the PHP handler
2. The web server and PHP preprocessor first determine whether the size of the form data exceeds the post_max_size# in the php.ini configuration file ##The upper limit value set by the option. If it exceeds, the PHP handler will not be able to obtain any form data. At this time, not only the file upload will fail, but the data filled in the form control will also fail to be submitted, that is to say: the PHP handler predefined variables $_GET, $_POST, $_FILES will be an empty array.
If it does not exceed the limit, the file will be uploaded to step 3 for verification.
3. Check whether the file size in the form exceeds the upper limit set by the form hidden field
MAX_FILE_SIZE. If it exceeds, the PHP preprocessor returns status code 2 and the file upload fails.
If it does not exceed the limit, the file will be uploaded to step 4 for verification.
(When there are multiple files to upload, the file upload failure caused by a certain file upload box will not affect the upload results of other file upload boxes)
4. Check whether the files in the form exceed the php.ini configuration file
upload_max_filesizeThe upper limit value set by the option. If it exceeds, the PHP preprocessor returns status code 1 and the file upload fails.
If it does not exceed the limit, the file will be uploaded to step 5 for verification.
5. To implement file upload in PHP, you need to create a temporary file corresponding to the uploaded file one-to-one (the default extension is tmp) in the directory defined by the upload_tmp_dir option in the php.ini configuration file. After the upload is successful, the temporary file disappears immediately. At this time, the PHP preprocessor returns status code 0.
But sometimes due to default reasons (such as the max_execution_time option setting is too small or the network speed is slow, etc.), after uploading some files, the remaining files are no longer uploaded, causing the file upload to fail. At this time, the PHP preprocessor returns status code 3
If passed, the file will be uploaded to step 6 for inspection.
6. The key step to achieve file upload is to save the temporary file to the web server or file server before the temporary file disappears. The two functions provided by PHP:
is_uploaded_file() function and move_uploaded_file() function can help complete this step


When uploading multiple files, please note that the contents of files saved with the same name are placed in the array in the following form. They are five arrays, stored separately according to the five parameters of the file, not three arrays. So if you use count($_FILES[‘$myPicture’]) directly, the answer is 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
Copy after login
Copy after login

index.php file

<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>
Copy after login
Copy after login

fileSystem file

<?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;
        } 
    }   
?>
Copy after login
Copy after login

Related recommendations:

php3.2 to achieve multiple File upload

A method to implement multi-file upload in PHP Example analysis

Example analysis PHP single file and multiple file upload

The above is the detailed content of How to implement multiple file uploads in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template