Php multi-file upload methods

Source: Internet
Author: User
& Lt ;? Php introduces three multifile Upload methods. The first two methods are to directly traverse the 3D Ultra-Global Array $ _ FILES. The last one is to define a single file upload function, this function is called repeatedly to complete multifile Upload. 1. when the name attribute of multiple uploaded files in a multifile Upload form is in the & ldquo; pic [] & rdquo; format, that is, the form is in the following format: & lt; formaction & quot; & quot
 0) {switch ($ up_info ['error'] [$ I]) {case 1: $ err_info = "the uploaded file exceeds php. value restricted by the upload_max_filesize option in ini "; break; case 2: $ err_info =" the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form "; break; case 3: $ err_info = "only part of the file is uploaded"; break; case 4: $ err_info = "no file is uploaded"; break; case 6: $ err_info = "temporary folder not found"; break; case 7: $ err_info = "file write failed"; break; default: $ err_info = "unknown Upload error "; break;} continue ($ err_info);} // 3. determine whether the file upload type is legal if (! In_array ($ up_info ['type'] [$ I], $ typelist) {continue ('file type error! '. $ Up_info ['type'] [$ I]);} // 4. filter the size of the uploaded file if ($ up_info ['size'] [$ I]> 1000000) {continue ('file size exceeds 1000000 ');} // 5. upload file name processing $ exten_name = pathinfo ($ up_info ['name'] [$ I], PATHINFO_EXTENSION); do {$ main_name = date ('ymhis '. '--'. rand (100,999); $ new_name = $ main_name. '. '. $ exten_name;} while (file_exists ($ to_path. '/'. $ new_name); // 6. determine whether the file is being uploaded and execute the upload if (is_uploaded_file ($ up_info ['tmp _ name'] [$ I]) {if (move_uploaded_file ($ up_info ['TM P_name '] [$ I], $ ob_path.'/'. $ new_name) {echo' file uploaded successfully! ';} Else {echo' an error occurred while moving the uploaded file! ';}} Else {echo' The file is not the uploaded file '; }}// brackets for the for loop 2. when the name attribute of multiple uploaded files in a multifile Upload form is in the format of "pic, text, doc, and pdf", that is, the form is in this form: you can also use foreach to upload multiple files cyclically: // 1. get the information of the file to be uploaded $ up_info =$ _ FILES; $ ob_path = ".. \ system "; $ typelist = array (" image/gif "," image/jpeg "," image/pjpeg "," image/png "); // define the running Upload file type foreach ($ up_info as $ k_name => $ value) {// use foreach to process the upload of multiple files cyclically. // 2. determine whether the file is uploaded incorrectly. if ($ up_info [$ k_name] ['error']> 0) {switch ($ up_info ['error'] [$ I]) {case 1: $ err _ Info = "the uploaded file exceeds php. value restricted by the upload_max_filesize option in ini "; break; case 2: $ err_info =" the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form "; break; case 3: $ err_info = "only part of the file is uploaded"; break; case 4: $ err_info = "no file is uploaded"; break; case 6: $ err_info = "temporary folder not found"; break; case 7: $ err_info = "file write failed"; break; default: $ err_info = "unknown Upload error "; break;} continue ($ err_info);} // 3. determine whether the file upload type is legal if (! In_array ($ up_info [$ k_name] ['type'], $ typelist) {continue ('file type error! '. $ Up_info [$ k_name] ['type']);} // 4. filter the size of the uploaded file if ($ up_info [$ k_name] ['size']> 100000) {echo 'the file size exceeds **'; continue;} // 5. upload file name processing $ exten_name = pathinfo ($ up_info [$ k_name] ['name'], PATHINFO_EXTENSION); do {$ main_name = date ('ymhis '. '--'. rand (100,999); $ new_name = $ main_name. '. '. $ exten_name;} while (file_exists ($ to_path. '/'. $ new_name); // 6. determine whether the file is being uploaded and execute the upload if (is_uploaded_file ($ up_info [$ k_name] ['tmp _ name']) {if (move_uploaded _ File ($ up_info [$ k_name] ['tmp _ name'], $ ob_path. '/'. $ new_name) {echo 'file uploaded successfully! ';} Else {echo' an error occurred while moving the uploaded file! ';}} Else {echo' The file is not the uploaded file '; }}// for loop brackets // 3. the above loop is embedded in the file upload code, which may be a bit difficult to understand. Here we provide the third method, // first define the function for uploading a single file, and then call this function again, each time a file is uploaded, // It can also be used to upload multiple files. this method is easier to understand. // The form to be uploaded is the same as that in the second method, that is, // The definition function is as follows: /** single-file upload function * @ param array $ up_info records the ultra-global array of uploaded file information * @ param string $ to_path the destination path of the file to be uploaded * @ param array $ typlist allowed file type * @ param int $ file_size size * @ return string Upload failed, returns the cause of failure. ** if the result is successful, the path including the file name is returned. */Function upload_fun ($ up_info, $ to_path, $ typelist = array (), $ file_size = 2000000) {// 1. determine whether a file is uploaded incorrectly if ($ up_file ['error']> 0) {switch ($ up_info ['error']) {case 1: $ err_info = "the uploaded file exceeds php. value restricted by the upload_max_filesize option in ini "; break; case 2: $ err_info =" the size of the uploaded file exceeds the value specified by the MAX_FILE_SIZE option in the HTML form "; break; case 3: $ err_info = "only part of the file is uploaded"; break; case 4: $ err_info = "no file is uploaded"; break; case 6: $ err_info = "temporary folder not found"; break; case 7: $ err_info = "file Failed to write "; break; default: $ err_info =" unknown Upload error "; break;} return $ err_info; die () ;}// 2. determine whether the uploaded file type is legal if (count ($ typelist)> 0) {if (! In_array ($ up_info ['type'], $ typelist) {$ err_info = 'the file type is invalid! '. $ Up_info ['type']; return $ err_info; die () ;}// 4. determine whether the size of the uploaded file exceeds the allowed value if ($ up_info ['size']> $ file_size) {return $ err_info = 'File size exceeds '. $ file_size; die () ;}// 5. rename the uploaded file $ exten_name = pathinfo ($ up_info ['name'], PATHINFO_EXTENSION); do {$ main_name = date ('ymhis '. '--'. rand (100,999); $ new_name = $ main_name. '. '. $ exten_name;} while (file_exists ($ to_path. '/'. $ new_name); // 6. determine whether the file is uploaded and move the file if (is_uploaded_file ($ up_info ['tmp _ name']) {I F (move_uploaded_file ($ up_info ['tmp _ name'], $ to_path. '/'. $ new_name) {$ err_info = $ to_path. '/'. $ new_name; return $ err_info; die ();} else {return $ err_info = 'an error occurred while moving the uploaded file! '; Die () ;}} else {return $ err_info =' This file is not an uploaded file! '; Die () ;}// the function usage is as follows $ multi_info =$ _ FILES; // receives all the upload information for $ _ FILES $ to_path = 'system '; // the target path of the file to be uploaded: foreach ($ multi_info as $ name => $ up_info) {// foreach traversal $ multi_info $ err_info = upload_fun ($ up_info, $ to_path ); // call the single-file upload function p ($ err_info); // output the returned information;} // this article ends at www.15597.com.?>

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.