PHP multi-file upload operations, _ PHP Tutorial

Source: Internet
Author: User
Tags php file upload
PHP multi-file upload operation ,. PHP multi-file upload operations: in the previous article, I talked about the PHP file Upload principle and simple operation example: single file upload. Www.cnblogs.comlichenweip3879566.html in fact, PHP multifile Upload operations,

In the previous article, I talked about the php file Upload principle and simple operation example: single file upload.

Http://www.cnblogs.com/lichenwei/p/3879566.html

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    
         
  Upload files:
  
  
Upload files:
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] = & gt; 0 [size] = & gt; 385150) [1] => Array ([name] => photo 2.jpg [type] => image/jpeg [tmp_name] => F: \ wamp \ tmp \ php13D2. tmp [error] = & gt; 0 [size] = & gt; 242043) [2] => Array ([name] => photo 3.jpg [type] => image/jpeg [tmp_name] => F: \ wamp \ tmp \ php13D3. tmp [error] => 0 [size] => 488293 ))

The code for splitting and reorganizing arrays is as follows:

 ?>

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

The code is as follows:

 0) {// indicates the error code of the uploaded file. The value of switch ($ fileError) {case 1: $ message = "the uploaded file exceeds the value specified by 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 write failed "; break; case 8: $ message =" file Upload interrupted due to PHP extension "; break ;} exit ("file Upload failed :". $ message) ;}if (! Is_uploaded_file ($ tempName) {// Determine whether the file is POST-uploaded ("not uploaded through http post");} else {if (! In_array ($ fileType, $ typeList) {exit ("the uploaded file is not of the specified type");} else {if (! Getimagesize ($ tempName) {// prevents users from uploading malicious files. for example, change the virus file extension to the image format exit ("the uploaded file is not an image ");}} if ($ fileSize> 1000000) {// specifies the size of the file to be uploaded in a specific form. exit ("The size of the uploaded file exceeds the limit ");} else {// avoid garbled Chinese names for uploaded files $ fileName = iconv ("UTF-8", "GBK", $ fileName ); // Convert the character encoding captured by iconv from UTF-8 to gbk and output $ fileName = str_replace (". ", time (). ". ", $ fileName); // add a timestamp after the image name to avoid overwriting if (move_uploaded_file ($ tempName," uploads /". $ fileName) {echo "file uploaded successfully! ";}Else {echo" file Upload failed ";}}}?>

The effect is as follows:




Tips: in the previous article, I talked about the php file Upload principle and simple operation example: upload a single file. Http://www.cnblogs.com/lichenwei/p/3879566.html actually...

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.