PHP multi-file 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 [].
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Run $ _ FILES to print the file in upload. php.
<?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:
<?php //print_r($_FILES['file']); $arr=$_FILES['file']; $files=array(); for($i=0;$i<count($arr['name']);$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:
<? Php // print_r ($ _ FILES ['file']); $ arr = $ _ FILES ['file']; $ files = array (); for ($ I = 0; $ I <count ($ arr ['name']); $ I ++) {// count () count the length of the array key value name $ 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 <count ($ files); $ 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 "); // define the allowed type if ($ fileError> 0) {// determine the Upload File error number switch ($ fileError) {case 1: $ message = "the uploaded file exceeds php. the value restricted by the upload_max_filesize option in 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: