PHP multi-File Upload Analysis

Source: Internet
Author: User

PHP multi-File Upload I personally understand that multi-File Upload is a basic application in PHP. I would like to give you a detailed introduction here. I have a limited level. Please give more comments. Everyone makes progress together.
Add [] to the name of the input Tag FILE type of the html file to create an array in the html file. For example, the name is pictures, the multi-file reference name is pictures [], and the instance is as follows:
<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> // example in the manual.
The methods for creating Arrays Using HTML files are described as follows:

To send your <form> result to a PHP script as an array, you must name the <input>, <select>, or <textarea> unit as follows:
<Input name = "MyArray []"/>
<Input name = "MyArray []"/>
<Input name = "MyArray []"/>
<Input name = "MyArray []"/>
Note 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 generates two arrays, MyArray and mytherarray, and sends them to the PHP script.


You can also assign the specified key name to the array:
<Input name = "AnotherArray []"/>
<Input name = "AnotherArray []"/>
<Input name = "AnotherArray [email]"/>
<Input name = "AnotherArray [phone]"/>
The AnotherArray will contain key names 0, 1, email, and phone.

Note: The key name of the specified array is optional in HTML. If no key name is specified, the array is filled in the order in which the Unit appears in the form. The first example contains key names 0, 1, 2, and 3.


After you Click Upload in the preceding HTML file, you can use the $ _ FILES super global variable to read the file. $ _ FILES will combine the File Information Group transmitted by the http post method into an array in the form of an array of two-dimensional FILES. The array of more than two files is three-dimensional.


$ _ FILES usage:
$ _ FILES ['userfile'] ['name']
The original name of the client machine file.
$ _ FILES ['userfile'] ['type'] MIME type of the file, if the browser provides this information. One example is"Image/gif". However, this MIME type is not checked on the PHP end, so do not take it for granted.
$ _ FILES ['userfile'] ['SIZE'] size of the uploaded file, in bytes.
$ _ FILES ['userfile'] ['tmp _ name'] temporary file name stored on the server after the file is uploaded.
$ _ FILES ['userfile'] ['error']


The following is a file upload. After this file is uploaded, the $ _ FILES array will generate its information. Because it is a file, it is two-dimensional, to help you understand the structure of $ _ FILES, use PRINT_R output to 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 receiving file:         
<?php
print_r($_FILES);
?>
Save it as UPLOAD. PHP and run the above HTML to select a file to UPLOAD. For example, if the file name is Thumbs. db, click View and source code in IE browser ". Shown as follows:
Array
(
[Pictures] => Array
(
[Name] => Thumbs. db // original file name
[Type] => application/octet-stream // file type
[Tmp_name] => D: EasyPHP \ tmpphp64.tmp // temporary storage directory and file name
[Error] => 0 // error code 0 indicates that the upload is successful.
[Size] => 23040 // File size
)
) According to this information, combined with the usage of the above $ _ FILES, you should be able to understand more deeply.


Next let's take a look at Multifile upload. The three file names are FILE1.TXT, FILE2.TXT, and FILE3.TXT respectively, and then use PRINT_R output to view their structure:

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 Receiving File Code:         
<?php
print_r($_FILES);
?>
View source files:

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] = & gt; 94289
[1] => 65536.
[2] => 102400.
)
)
)
If the FILES named/file1.txt and/file2.txt are submitted, the value of $ _ FILES ['pictures'] ['name'] [0] will be file1.txt, the value of $ _ FILES ['pictures'] ['name'] [1] is file2.txt. When '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.