PHP Multi-File Upload implementation

Source: Internet
Author: User
As long as you name the file upload tag in the form as an array, you can upload multiple files simultaneously.

Let's look at an example:
---------------------------------------------------------------------
  1. function upload ($file _error, $file _tmp_name, $file _name) {
  2. $info = "";
  3. if ($file _name = = "")
  4. return $info;
  5. Switch ($file _error) {
  6. Case Upload_err_ini_size:
  7. $info = $file _name. ": The file size exceeds the server limit";
  8. Break
  9. Case Upload_err_form_size:
  10. $info = $file _name. ": The file size exceeds the browser limit";
  11. Break
  12. Case Upload_err_partial:
  13. $info = $file _name. ": Only part of the file was uploaded";
  14. Break
  15. Case Upload_err_no_file:
  16. $info = $file _name. ": No files were uploaded";
  17. Break
  18. Case UPLOAD_ERR_NO_TMP_DIR:
  19. $info = $file _name. ": Unable to find temp folder";
  20. Break
  21. Case Upload_err_cant_write:
  22. $info = $file _name. ": File write Failed";
  23. Break
  24. Case UPLOAD_ERR_OK:
  25. $upload _dir = './'. Iconv ("UTF-8", "gb2312", $file _name);
  26. if (file_exists ($upload _dir)) {
  27. $info = $file _name. ": File with the same name already exists";
  28. }else{
  29. if (Move_uploaded_file ($file _tmp_name, $upload _dir)) {
  30. $info = $file _name. ": File upload succeeded";
  31. }else{
  32. $info = $file _name. ": File upload Failed";
  33. }
  34. }
  35. Break
  36. }
  37. return $info;
  38. }
  39. if (Isset ($_post[' submit ')) {
  40. $info = ";
  41. $count = count ($_files[' upload_file ' [' name ']);
  42. for ($i =0; $i < $count; + + $i) {
  43. if ($_files[' upload_file ' [' name '] [$i] = = "")
  44. Continue
  45. $info = Upload (
  46. $_files[' upload_file ' [' Error '] [$i],
  47. $_files[' upload_file ' [' tmp_name '] [$i],
  48. $_files[' upload_file ' [' name '] [$i]
  49. );
  50. }
  51. Echo $info;
  52. }
  53. ?>
Copy Code

--------------------------------------------------------------------------------------
The code execution results are as follows:


Attention:

1., name= "upload_file[" must be named as an array, or there will be an error: "Uninitialized string offset:0", which means that your array key value crosses the line

2, $_files[' upload_file ' [' name '] [$i], Upload_file is the name of the upload file marker in the form, and when multiple files are uploaded, the bidding clubs of $_files in the third dimension of the array is automatically numbered starting from 0.
File Upload, PHP
  • 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.