Php multi-file upload-PHP source code

Source: Internet
Author: User
Php multifile Upload Method 1: when the name attribute of multiple uploaded files in the multifile Upload form is in the "pic []" format, that is, the form is in this form.

// Multifile_upload () {// 1. get the information of the file to be uploaded $ up_info =$ _ FILES ['Pic ']; $ ob_path = "images_c"; $ typelist = array ("image/gif ", "image/jpeg", "image/pjpeg", "image/png"); // defines the running Upload file type for ($ I = 0; $ I
 
  
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 Valid 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,99999); $ 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_fi Le ($ up_info ['tmp _ name'] [$ I], $ ob_path. '/'. $ new_name) {// echo 'file uploaded successfully! '; $ Img [] = $ new_name;} else {echo' an error occurred while moving the uploaded file! ';}} Else {echo' The file is not the uploaded file '; }}// for loop parentheses return $ img;} // Method 1: when the name attribute of multiple uploaded files in a multifile Upload form is in the "pic []" format, that is, the form is in this form.
 

Method 2: when the name attribute of multiple uploaded files in the multifile Upload form is in the "pic, text, doc, pdf" format

// 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. the value restricted by the upload_max_filesize option in ini "; break; case 2: $ err_info =" the size of the uploaded file exceeds the value of MAX_FILE _ in the HTML form _ The value specified by the SIZE option "; 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 66 67 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 '; }}// brackets for the for loop // Method 2: when the name attribute of multiple uploaded files in a multifile Upload form is in the format of "pic, text, doc, and pdf ",

Method 3: define the function for uploading a single file, and call this function again. each time a file upload information is imported, the function can be uploaded to multiple files.

/* 002 * single file upload function 003 * @ param array $ up_info the ultra-global array 004 * @ param string $ to_path the target path for file Upload 005 * @ param array $ typlist: the file type 006 * @ param int $ file_size: the file size 007 * @ return string failed to be uploaded, returns the cause of failure, 008 * http://yige.org/php/009 * The path including the file name is returned. 010 */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"; brea K; case 7: $ err_info = "file writing failed"; 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 it is an uploaded file and move the file if (is_uploaded_file ($ up_info ['Tmp _ name']) {if (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;} // method 3: define the function for uploading a single file and call this function again, each time you upload a file, you can also upload multiple files. this method is easier to understand. ------------------------------------------------------------------

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.