A method of implementing multi-file uploading in PHP

Source: Internet
Author: User
This article is mainly to share a PHP implementation of a multi-file upload method, before the implementation of the form file type input select multiple images of the time to find a way may not be the best but the pro-test is feasible and support IE7 above and Chrome browser, hope to help everyone.

Select the Multiple property with normal multiple files in the form


<input type= "file" id= "image" class= "file image hidden" name= "image[]" multiple= "true" >

Then use Ajaxfileupload or other means to submit

Convert named file files $file[‘image'] to JSON print

Normal format


{"Name": "7332.png", "type": "Image\/png", "Tmp_name": "\/TMP\/PHPLQPPVR", "error": 0, "Size": 659}

But at this point the result is


{"Name": ["7656.png", "7718.png"], "type": ["Image/png", "Image/png"], "tmp_name": ["/TMP/PHPDZSOVJ", "/TMP/PHPP8KWMT" ], "error": [0,0], "size": [357,662]}

All of the properties are sorted into arrays in order

You can use the following code to save the picture


if (!isset ($_files[$field])) {return new Jsonresponse (' ErrorCode ' =>1, ' message ' = ' please upload file ')}  Rename $_file store multiple files upload $arrayfile = Array (); foreach ($_files[$field] as $key + $value) {$i = 0;      if (Is_array ($value)) {foreach ($value as $v) {$i + +; After renaming, re-placing the Super global variable _file ensure that the key name is unique and can be uploaded directly $name = $field. ' _split_ '.      $i;    $_files[$name] [$key] = $v; }}}//whether to upload multiple files if ($i > 0) {for ($j = 1; $j <= $i; $j + +) {Array_push ($arrayFile, $field. ' _split_ '. $J); }}else{Array_push ($arrayFile, $field);}//Traverse file Multiple Files upload foreach ($arrayFile as $file) {if (Isset ($_files[$file]) &AMP;&A mp    $_files[$file [' name ']) {//Custom upload method specific content slightly $data = $this->uploadfile ($file, $path, Uniqid ());        if (Isset ($data) &&!empty ($data)) {if (!isset ($data [' errors ')]) {//upload results stored in $result multiple picture addresses using comma concatenation if (Isset ($result)) {$result = array (' ErrorCode ' =>0, ' message ' = = $result [' message ']. ',' .        Reset ($data)); }else{$result = array(' ErrorCode ' =>0, ' message ' =>reset ($data));        }}else{//The following is the return error message if (Is_array (Reset ($data))) {$message = reset ($data) [0];        }else{$message = reset ($data);      } $result = Array (' errorCode ' = = 1, ' message ' = = $message);      }} else {$result = array (' ErrorCode ' =>1, ' message ' = ' upload failed ');    Break    }} else {$result = array (' ErrorCode ' =>1, ' message ' = ' please upload file ');  Break }}//return the upload result return $result;

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.