PHP multiple File Upload

Source: Internet
Author: User
Tags arrays manual error code file size file upload http post php script

Multi-File upload is a basic PHP application, the relevant knowledge to everyone in detail, I have a limited level, but also please give more advice. Everybody progress together.
Upload files HTML input tag in the file type after the name to add [], the role is in the HTML to create an array of PHP, such as the name is pictures, multiple file reference name is pictures[], the example is as follows:
Copy Content to Clipboard


<form action= "upload.php" method= "post" enctype= "Multipart/form-data" >
<p>
<input type= "File" Name= "pictures[]"/><br/>
<input type= "File" Name= "pictures[]"/><br/>
<input type= "File" Name= "pictures[]"/><br/>
<input type= "Submit" value= "Upload"/>
</p>
</form>//manual examples.
The method manual for building arrays using HTML files is explained in this way:

To get your <form> results to be sent to the PHP script as an array, name the <input>,<select> or <textarea> unit:
<input name= "myarray[]"/>
<input name= "myarray[]"/>
<input name= "myarray[]"/>
<input name= "myarray[]"/>
Notice the square brackets after the variable name, which makes it an array.


You can group cells into different arrays by assigning the same names to different cells:
<input name= "myarray[]"/>
<input name= "myarray[]"/>
<input name= "myotherarray[]"/>
<input name= "myotherarray[]"/>
This will produce two arrays, myarray and Myotherarray, and sent to the PHP script.


You can also assign a specified key name to an array:
<input name= "anotherarray[]"/>
<input name= "anotherarray[]"/>
<input name= "Anotherarray[email]"/>
<input name= "Anotherarray[phone]"/>
The Anotherarray array will contain the key name 0,1,email and phone.

Note: The key name for the specified array is an optional HTML option. If you do not specify a key name, the array is filled in the order in which the cells appear in the form. The first example will contain the key name 0,1,2,3.


Above HTML click Upload, through $_files super global variable to read, $_files will send the HTTP POST method to the file information group to synthesize the array, a file array form two-dimensional. More than two file arrays form three-dimensional.


How to use $_files:
$_files[' UserFile ' [' Name ']
The original name of the client machine file.
$_files[the MIME type of the ' userfile ' [' type '] file if the browser provides this information. One example is "Image/gif". However, this MIME type is not checked on the PHP side, so do not assume that this is the value.
$_files[' userfile ' [' size '] the size of the uploaded file, in bytes.
$_files[' UserFile ' [' tmp_name '] files are stored in the server-side temporary file name after being uploaded.
$_files[' userfile ' [' Error ']


Here is a file upload, this file upload $_files array will produce its information, because it is a file is two-dimensional, in order to facilitate understanding we will $_files with Print_r output view its structure.

HTML file:



<form action= "upload.php" method= "post" enctype= "Multipart/form-data" >
<p>
<input type= "file" name= "pictures"/><br/>
<input type= "Submit" value= "Upload"/>
</p>
</form>
PHP Receive file:



<?php
Print_r ($_files);
?>
Save it as upload.php, run the HTML above to select a file upload, such as file name Thumbs.db, click "View" in IE browser, "source code". The display is as follows:
Array
(
[Pictures] => Array
(
[Name] => thumbs.db//original filename
[Type] => application/octet-stream//file type
[Tmp_name] => d:easyphp\tmpphp64.tmp//temporary storage directory and filename
[ERROR] => 0//error code 0 for upload success
[Size] => 23040//File size
)
According to this information, combined with the use of the above $_files, we should be able to understand more deeply.


Below look at the multiple file upload, three file names are file1.txt,file2 respectively. Txt,file3. TXT, and then view its structure with Print_r output:

HTML file Code:



<form action= "upload.php" method= "post" enctype= "Multipart/form-data" >
<p>
<input type= "File" Name= "pictures[]"/><br/>
<input type= "File" Name= "pictures[]"/><br/>
<input type= "File" Name= "pictures[]"/><br/>
<input type= "Submit" value= "Upload"/>
</p>
</form>
PHP receives file code:



<?php
Print_r ($_files);
?>
To view the source file:

Array
(
[Pictures] => Array
(
[Name] => Array
(
[0] => File1.txt
[1] => File2.txt
[2] => File3.txt
)
[Type] => Array
(
[0] => Application/octet-stream
[1] => Application/octet-stream
[2] => Application/octet-stream
)
[Tmp_name] => Array
(
[0] => d:easyphp\tmpphp47.tmp
[1] => d:easyphp\tmpphp48.tmp
[2] => d:easyphp\tmpphp49.tmp
)
[ERROR] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[Size] => Array
(
[0] => 94289
[1] => 65536
[2] => 102400
)
)
)
         Assume that documents named/file1.txt   and/file2.txt are submitted, then $_files[' Pictures ' [' Name '][0 The value will be file1.txt, and the value of $_files[' pictures ' [' name '][1] will be file2.txt. Similarly, $_files[' file2.txt ' [' Size '][0] will contain the size of the file file1.txt, and so on.

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.