PHP upload multiple files in the form design example _php tips

Source: Internet
Author: User
Tags php script

Multiple file uploads and individual file uploads are handled the same way, simply by providing several input forms of type "file" to the client and specifying different "name" property values. For example, in the following code, you can have the user select three local files to pass to the server together, and the client's form looks like this:

Copy Code code as follows:

<body>
<form action= "mul_upload.php" method= "post" enctype= "Multipart/form-data" >
<input type= "hidden" name= "max_file_size" value= "1000000" >
Select File 1:<input type= "file" Name= ' myfile[] ' ><br>
Select File 2:<input type= "file" Name= ' myfile[] ' ><br>
Select File 3:<input type= "file" Name= ' myfile[] ' ><br>
<input type= "Submit" value= "Upload file" >
</form>
</body>

In the above code, the forms of three file types are organized together in an array. When the above form teaches PHP script file mul_upload.php, the server side also uses the global array $_files to store all of the above file information, but $_files from the two-dimensional array has been converted to three-dimensional array, so that can store multiple uploaded file information. In the script file mul_upload.php, use the Print_r () function to output the contents of the $_files array, as shown in the following code:

Copy Code code as follows:

<?php
Print the contents of the three-dimensional array $_files to see the structure of the stored upload file
Print_r ($_files);
?>

When three local file submissions are selected, the output looks like this:

Copy Code code as follows:

Array (
[Myfile]=>array (
[Name]=>array (---$_files["myfile"] ["name"] stores the contents of all uploaded files
[0]=>rav.ini---$_files["myfile"] ["name"][0] The name of the first upload file
[1]=>msgsocm.log---$_files["myfile"] ["name"][1] The name of the second upload file
[2]=>notepad. EXE)---$_files["myfile" ["Name"][2] The name of the third upload file
[Type]=>array (---$_files["myfile"] ["type"] stores all types of uploaded files
[0]=>application/octet-stream---$_files["myfile"] ["type"][0] the type of the first upload file
[1]=>application/octet-stream---$_files["myfile"] ["type"][1] The type of the second upload file
[2]=>application/octet-stream)---$_files["myfile" ["type"][2] The third type of upload file
[Tmp_name]=>array (
[0]=>c:\windows\temp\phpaf.tmp
[1]=>c:\windows\temp\phpb0.tmp
[2]=>c:\windows\temp\phpb1.tmp)
[Error]=>array (
[0]=>0
[1]=>0
[2]=>0)
[Size]=>array (
[0]=>64
[1]=>1350
[2]=>66560)]
)

By outputting the value of the $_files array, you can see that the processing of multiple file uploads is the same as when a single file is uploaded, except that the structure of the $_files array is slightly different. This way you can support a larger number of file uploads.

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.