How PHP implements multiple file uploads

Source: Internet
Author: User
Tags php file upload

This article mainly to share with you how PHP implementation of multi-file upload, I hope to help everyone.

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 the PHP preprocessor first determine whether the size of the form data exceeds the upper limit set by the post_max_size option in the php.ini configuration file.
If not, the PHP handler will not be able to get any form data, not only upload the file failed, and the form control fill in the data will also fail to commit, that is to say: PHP handler predefined variables $_get, $_post, $_files will be an empty array.
If not, file upload into the 3rd step test.
3. Verify that the file size in the form exceeds the upper limit of the max_file_size setting for the hidden field in the form.
If the PHP preprocessor returns a status code of 2, the file upload fails.
If not, file upload into the 4th step test.
(when there are multiple files to upload, a file upload box caused by the file upload failure, will not affect the upload results of other file upload box)
4. Verify that the file in the form exceeds the upper value set by the upload_max_filesize option in the PHP.ini profile.
If the PHP preprocessor returns a status code of 1, the file upload fails.
If not, file upload into the 5th step test.
5. The PHP implementation upload file needs to create a temporary file corresponding to the upload file one by one in the directory defined in the PHP.ini profile upload_tmp_dir option (the default extension is TMP), after the upload succeeds, the temporary file disappears immediately, and the PHP preprocessor returns status code 0.
However, sometimes due to the reasons for the dictation (such as the max_execution_time option is too small or slow, etc.), upload some files will not continue to upload the remaining files, resulting in file upload failure, the PHP preprocessor return status Code 3
If passed, the file upload into the 6th step test.
6. A key step in implementing file uploads is to save the temporary file to a Web server or file server before the temporary file disappears. PHP provides two functions: theis_uploaded_file () function and the move_uploaded_file () function, which can help to accomplish this step

Multiple file uploads note that the same name holds the contents of the file in the array as shown in the following form. is five arrays, which are stored 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 ' (Leng th=5)      2 = String ' 3.txt ' (length=5)  ' type ' = =     Array (size=3)      0 = String ' text/plain ' (length=      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< c23/>1 = int 0      2 = int 0

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= "" maxlength= "[+] ><br >    <input type= "file" name= "mypicture[" "size=" "maxlength=" ["]" ><br>    <input type= "File" Name= "mypicture[" "size=" "maxlength=" ><br>    <input type= "Submit" value= "Submit" ></form>

FileSystem file

<?php if (empty ($_post)) {exit ("the submitted form data exceeds the configuration of 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 "Upload success";            Break                Case 1:echo "Upload attachment exceeds the limit of upload_max_filesize option in php.ini";            Break                Case 2:echo "The size of the upload attachment exceeds the value specified by the Form form max_file_size option"; BReak;                Case 3:echo "Attachments are only partially uploaded";            Break                Case 4:echo "no option to upload attachments";        Break }}?>

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 the PHP preprocessor first determine whether the size of the form data exceeds the upper limit set by the post_max_size option in the php.ini configuration file.
If not, the PHP handler will not be able to get any form data, not only upload the file failed, and the form control fill in the data will also fail to commit, that is to say: PHP handler predefined variables $_get, $_post, $_files will be an empty array.
If not, file upload into the 3rd step test.
3. Verify that the file size in the form exceeds the upper limit of the max_file_size setting for the hidden field in the form.
If the PHP preprocessor returns a status code of 2, the file upload fails.
If not, file upload into the 4th step test.
(when there are multiple files to upload, a file upload box caused by the file upload failure, will not affect the upload results of other file upload box)
4. Verify that the file in the form exceeds the upper value set by the upload_max_filesize option in the PHP.ini profile.
If the PHP preprocessor returns a status code of 1, the file upload fails.
If not, file upload into the 5th step test.
5. The PHP implementation upload file needs to create a temporary file corresponding to the upload file one by one in the directory defined in the PHP.ini profile upload_tmp_dir option (the default extension is TMP), after the upload succeeds, the temporary file disappears immediately, and the PHP preprocessor returns status code 0.
However, sometimes due to the reasons for the dictation (such as the max_execution_time option is too small or slow, etc.), upload some files will not continue to upload the remaining files, resulting in file upload failure, the PHP preprocessor return status Code 3
If passed, the file upload into the 6th step test.
6. A key step in implementing file uploads is to save the temporary file to a Web server or file server before the temporary file disappears. PHP provides two functions: theis_uploaded_file () function and the move_uploaded_file () function, which can help to accomplish this step

Multiple file uploads note that the same name holds the contents of the file in the array as shown in the following form. is five arrays, which are stored 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 ' (Leng th=5)      2 = String ' 3.txt ' (length=5)  ' type ' = =     Array (size=3)      0 = String ' text/plain ' (length=      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< c23/>1 = int 0      2 = int 0

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= "" maxlength= "[+] ><br >    <input type= "file" name= "mypicture[" "size=" "maxlength=" ["]" ><br>    <input type= "File" Name= "mypicture[" "size=" "maxlength=" ><br>    <input type= "Submit" value= "Submit" ></form>

FileSystem file

<?php if (empty ($_post)) {exit ("the submitted form data exceeds the configuration of 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 "Upload success";            Break                Case 1:echo "Upload attachment exceeds the limit of upload_max_filesize option in php.ini";            Break                Case 2:echo "The size of the upload attachment exceeds the value specified by the Form form max_file_size option"; BReak;                Case 3:echo "Attachments are only partially uploaded";            Break                Case 4:echo "no option to upload attachments";        Break }}?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.