Php example: how to upload multiple php files

Source: Internet
Author: User
Tags php example
This article mainly introduces the implementation of PHP file uploading as many files as possible. For more information, see Multifile Upload

① Use multiple name values

 

A. The data format received after clicking submit

Array([file1] => Array([name] => 8.png[type] => image/png[tmp_name] => G:\wamp\tmp\php737.tmp[error] => 0[size] => 200)[file2] => Array([name] => 28.png[type] => image/png[tmp_name] => G:\wamp\tmp\php738.tmp[error] => 0[size] => 6244)[file3] => Array([name] => 54a296f8n6787b34c.png[type] => image/png[tmp_name] => G:\wamp\tmp\php739.tmp[error] => 0[size] => 3143)[file4] => Array([name] => 54c0573dncb4db6f7.jpg[type] => image/jpeg[tmp_name] => G:\wamp\tmp\php788.tmp[error] => 0[size] => 5404))

It can be seen from this format that each file corresponds to an array unit.

Therefore, use foreach to traverse the array and call the file upload function for each array unit.

B. click submit.

① Receive uploaded file information

$ File = $ _ FILES;

② Introduce the upload function

include('./functions.php');

③ Set the file storage path

$ Path = './uploads/'; // you must manually create this directory.

④ Call the file upload function

Foreach ($ file as $ v) {$ info = uploadFile ($ v, $ path); ⑤ determine the upload status if ($ info ['isok']) {echo 'upload successful '. $ info ['message'];} else {echo 'upload failed '. $ info ['message'] ;}}

---------------------------------------------------------------

② Use a single name value

A. Method 1

 

B. Method 2

 

C. The data format received after clicking submit

Array([userpic] => Array([name] => Array([0] => 8.png[1] => 9b2d7581fba543ec9bcf95e91018915a.gif[2] => 12.jpg)[type] => Array([0] => image/png[1] => image/gif[2] => image/jpeg)[tmp_name] => Array([0] => G:\wamp\tmp\php85E5.tmp[1] => G:\wamp\tmp\php85E6.tmp[2] => G:\wamp\tmp\php8635.tmp)[error] => Array([0] => 0[1] => 0[2] => 0)[size] => Array([0] => 200[1] => 16503[2] => 19443)))

It can be seen from this format that the information of the uploaded files is stored separately in each subscript.
So what we need to do is to splice a complete file information, a one-dimensional array.

Array([name] => 54c0573dncb4db6f7.jpg[type] => image/jpeg[tmp_name] => G:\wamp\tmp\php788.tmp[error] => 0[size] => 5404)

Therefore, the operation is to traverse $ _ FILES ['file'] and retrieve the information of each uploaded file.

D. click submit.

① Receive uploaded file information

$ File = $ _ FILES ['file'];

② Introduce the upload function

Include ('./functions. php ');

③ Set the file storage path

$ Path = './uploads/'; // you must manually create this directory.

④ Call the file upload function

foreach($file['name'] as $key=>$value){$data['name'] = $file['name'][$key];$data['type'] = $file['type'][$key];$data['tmp_name'] = $file['tmp_name'][$key];$data['error'] = $file['error'][$key];$data['size'] = $file['size'][$key];$info = uploadFile($data,$path);

⑤ Determine the upload status

If ($ info ['isok']) {echo 'uploaded successfully '. $ info ['message'];} else {echo 'upload failed '. $ info ['message'] ;}}

A. Traverse $ file ['name'] only to get $ key

B. retrieve the file information corresponding to the underlying object every traversal and assign a value to the corresponding key in the new array.

For example, $ key = 0 for the first time;

$ Data ['name'] = $ file ['name'] [0]; // It is equivalent to extracting the name of the first file $ data ['type'] = $ file ['type'] [0]; // It is equivalent to extracting the type of the first file

...

After the first traversal

$data = array([name] => 54c0573dncb4db6f7.jpg[type] => image/jpeg[tmp_name] => G:\wamp\tmp\php788.tmp[error] => 0[size] => 5404);

In this way, all the information of the first file is retrieved.

Then, call the upload function to upload files.

The second time lasted $ key = 1, which is equivalent to obtaining the information of the second uploaded File

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.