PHP multi-file Upload _ PHP Tutorial

Source: Internet
Author: User
PHP multi-file upload operation. PHP multi-file upload operations are similar in terms of multifile Upload and single file upload. the principles are the same, but some tips have been made on the code. The first is the index.html upload form, which only supports PHP multi-file upload operations.

In fact, multi-file upload and single-file upload are similar in the same principle, but they only do some tips in code.

The first is the index.html upload form, which is used to change the file in the previously uploaded file form to file [].

Upload files

Run $ _ FILES to print the file in upload. php.

Print_r ($ _ FILES );

?>

Obtain the following multi-dimensional array

Array

(

[File] => Array

(

[Name] => Array

(

[0] => photo 1.jpg

[1] => photo 2.jpg

[2] => photo 3.jpg

)

[Type] => Array

(

[0] => image/jpeg

[1] => image/jpeg

[2] => image/jpeg

)

[Tmp_name] => Array

(

[0] => F: \ wamp \ tmp \ php36C7. tmp

[1] => F: \ wamp \ tmp \ php36C8. tmp

[2] => F: \ wamp \ tmp \ php36C9. tmp

)

[Error] => Array

(

[0] => 0

[1] => 0

[2] => 0

)

[Size] => Array

(

[0] => 0

[1] => 0

[2] => 0

)

)

)

Based on the principle of uploading a single file, first think about what we need?

Obviously, we need to get an array of file information, which contains name, type, tmp_name, error, and size. at this time, we get a multi-dimensional array, although the corresponding key value exists, it is multidimensional,

We only need to split it, for example, the above three files, we just need to split it into the corresponding three file information arrays.

Structure of split array

Array

(

[0] => Array

(

[Name] => photo 1.jpg

[Type] => image/jpeg

[Tmp_name] => F: \ wamp \ tmp \ php13C1. tmp

[Error] => 0

[Size] = & gt; 385150

)

[1] => Array

(

[Name] => photo 2.jpg

[Type] => image/jpeg

[Tmp_name] => F: \ wamp \ tmp \ php13D2. tmp

[Error] => 0

[Size] = & gt; 242043

)

[2] => Array

(

[Name] => photo 3.jpg

[Type] => image/jpeg

[Tmp_name] => F: \ wamp \ tmp \ php13D3. tmp

[Error] => 0

[Size] = & gt; 488293

)

)

The code for splitting and reorganizing arrays is as follows:

// Print_r ($ _ FILES ['file']);

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

$ Files = array ();

For ($ I = 0; $ I

$ Files [$ I] ['name'] = $ arr ['name'] [$ I];

$ Files [$ I] ['type'] = $ arr ['type'] [$ I];

$ Files [$ I] ['tmp _ name'] = $ arr ['tmp _ name'] [$ I];

$ Files [$ I] ['error'] = $ arr ['error'] [$ I];

$ Files [$ I] ['size'] = $ arr ['size'] [$ I];

}

Print_r ($ files );

?>

The rest is simple. repeat the upload steps of a single file and traverse and process the array.

The code is as follows:

// Print_r ($ _ FILES ['file']);

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

$ Files = array ();

For ($ I = 0; $ I

$ Files [$ I] ['name'] = $ arr ['name'] [$ I];

$ Files [$ I] ['type'] = $ arr ['type'] [$ I];

$ Files [$ I] ['tmp _ name'] = $ arr ['tmp _ name'] [$ I];

$ Files [$ I] ['error'] = $ arr ['error'] [$ I];

$ Files [$ I] ['size'] = $ arr ['size'] [$ I];

}

For ($ I = 0; $ I

// Obtain the information of the uploaded file

$ FileName = $ files [$ I] ['name'];

$ FileType = $ files [$ I] ['type'];

$ FileError = $ files [$ I] ['type'];

$ FileSize = $ files [$ I] ['size'];

$ TempName = $ files [$ I] ['tmp _ name']; // temporary file name

// Define the Upload file type

$ TypeList = array ("image/jpeg", "image/jpg", "image/png", "image/gif"); // defines the allowed types

If ($ fileError> 0 ){

// File upload error code determination

Switch ($ fileError ){

Case 1:

$ Message = "the uploaded file exceeds the limit of the upload_max_filesize option in php. ini. ";

Break;

Case 2:

$ Message = "the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form. ";

Break;

Case 3:

$ Message = "only part of the file is uploaded. ";

Break;

Case 4:

$ Message = "no file is uploaded. ";

Break;

Case 6:

$ Message = "The temporary folder cannot be found. ";

Break;

Case 7:

$ Message = "file writing failed ";

Break;

Case 8:

$ Message = "file Upload interrupted due to PHP extensions ";

Break;

}

Exit ("file Upload failed:". $ message );

}

If (! Is_uploaded_file ($ tempName )){

// Determine whether the object is uploaded by POST.

Exit ("not uploaded via http post ");

} Else {

If (! In_array ($ fileType, $ typeList )){

Exit ("the uploaded file is not of the specified type ");

} Else {

If (! Getimagesize ($ tempName )){

// Prevent users from uploading malicious files. for example, you can change the virus file extension to the image format.

Exit ("the uploaded file is not an image ");

}

}

'If ($ fileSize> 1000000 ){

// Limit the size of the file to be uploaded for a specific form

Exit ("The size of the uploaded file exceeds the limit ");

} Else {

// Avoid garbled Chinese names of uploaded files

$ FileName = iconv ("UTF-8", "GBK", $ fileName); // Convert the character encoding captured by iconv from UTF-8 to gbk output

$ FileName = str_replace (".", time (). ".", $ fileName); // add a timestamp after the image name to avoid overwriting the file with the same name

If (move_uploaded_file ($ tempName, "uploads/". $ fileName )){

Echo "file uploaded! ";

} Else {

Echo "failed to upload file ";

}

}

}

}

?>

In fact, the principle for uploading multiple files is similar to that for uploading a single file. The principle is the same, but it is just a little bit tricky in code. The first is the index.html upload form, only...

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.