Home > Backend Development > PHP Tutorial > PHP multi-file upload implementation

PHP multi-file upload implementation

WBOY
Release: 2016-07-25 08:44:23
Original
804 people have browsed it
As long as the file upload tag in the form is named in the form of an array, multiple files can be uploaded simultaneously.

Let’s look at an example:
-------------------------------------------------- -------------------
  1. Upload file:
    Upload file:
    Upload file: < ;/td>
  2. Upload file:
  3. function upload($file_error, $file_tmp_name, $file_name){
  4. $info = "";
  5. if($file_name == "")
  6. return $info;
  7. switch($file_error){
  8. case UPLOAD_ERR_INI_SIZE:
  9. $info = $file_name. ": The file size exceeds the server limit";
  10. break;
  11. case UPLOAD_ERR_FORM_SIZE:
  12. $info = $file_name. ": The file size exceeds the browser limit";
  13. break;
  14. case UPLOAD_ERR_PARTIAL:
  15. $info = $file_name. ": Only part of the file was uploaded";
  16. break;
  17. case UPLOAD_ERR_NO_FILE:
  18. $info = $file_name. ": None File uploaded";
  19. break;
  20. case UPLOAD_ERR_NO_TMP_DIR:
  21. $info = $file_name. ": Temporary folder not found";
  22. break;
  23. case UPLOAD_ERR_CANT_WRITE:
  24. $info = $file_name. ": File write failed" ;
  25. break;
  26. case UPLOAD_ERR_OK:
  27. $upload_dir = './'.iconv("UTF-8","gb2312",$file_name);
  28. if(file_exists($upload_dir)){
  29. $info = $file_name. ": The file with the same name already exists";
  30. }else{
  31. if(move_uploaded_file($file_tmp_name,$upload_dir)){
  32. $info = $file_name.": The file was uploaded successfully";
  33. }else{
  34. $info = $file_name. ": File upload failed";
  35. }
  36. }
  37. break;
  38. }
  39. return $info;
  40. }
  41. if(isset($_POST['submit'])){
  42. $info = '';
  43. $count = count($_FILES['upload_file']['name']);
  44. for($i=0; $i<$count; ++$i){
  45. if($_FILES['upload_file']['name' ][$i] == "")
  46. continue;
  47. $info = upload(
  48. $_FILES['upload_file']['error'][$i],
  49. $_FILES['upload_file']['tmp_name'] [$i],
  50. $_FILES['upload_file']['name'][$i]
  51. );
  52. }
  53. echo $info;
  54. }
  55. ?>
Copy code


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


Note:

1. In , name="upload_file[]" must be named in the form of an array, otherwise an error will occur: "Uninitialized string offset: 0", which means that your array key value is out of bounds

2. In $_FILES['upload_file']['name'][$i], upload_file is the name of the upload file marker in the form. When uploading multiple files, the third dimension subscript of the array $_FILES will automatically start from 0 Numbered in sequence.
File upload, PHP


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template