Home  >  Article  >  Backend Development  >  PHP sample code for uploading multiple files and images

PHP sample code for uploading multiple files and images

WBOY
WBOYOriginal
2016-07-25 08:57:461223browse
  1. $uptypes=array(
  2. //Upload file ContentType format
  3. 'image/jpg',
  4. 'image/jpeg',
  5. 'image/png',
  6. 'image/pjpeg',
  7. 'image/gif',
  8. 'image/bmp',
  9. 'image/x- png',
  10. 'application/msword',//doc
  11. 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',//docx
  12. 'application/vnd.openxmlformats-officedocument.presentationml.presentation',//pptx
  13. 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',//xlsx
  14. 'text/plain'
  15. );
  16. /**
  17. * PHP multi-file, multi-image upload
  18. * by bbs.it-home.org
  19. */
  20. $max_file_size=2000000; //Upload file size limit, unit BYTE
  21. $dir="upload/"; //Upload file path
  22. if ($_SERVER['REQUEST_METHOD'] == 'POST')
  23. {
  24. $file = $_FILES['upfile']['name'];
  25. foreach($file as $key=>$item){
  26. if($item != ''){
  27. if (!is_uploaded_file($_FILES['upfile']['tmp_name'][$key]))/ /Whether the file exists
  28. {
  29. echo "The picture does not exist!";
  30. exit;
  31. }
  32. if($max_file_size < $_FILES['upfile']['size'][$key])//Check the file size
  33. {
  34. echo "The file is too big!";
  35. exit;
  36. }
  37. if(!file_exists($dir))
  38. {
  39. mkdir($dir);
  40. }
  41. $filename=$_FILES['upfile']['tmp_name '][$key];
  42. $image_size = getimagesize($filename);
  43. $pinfo = pathinfo($file[$key]);
  44. $ftype = $pinfo['extension'];
  45. $destination = $dir .time().$file[$key];
  46. if (file_exists($destination) && $overwrite != true)
  47. {
  48. echo "The file with the same name already exists";
  49. exit;
  50. }
  51. if(!move_uploaded_file ($filename, $destination))
  52. {
  53. echo "Error moving file";
  54. exit;
  55. }
  56. $pinfo=pathinfo($destination);
  57. $fname=$pinfo['basename'];
  58. echo " < font color=red>has been successfully uploaded
    File name: ".$dir.$fname."
    ";
  59. echo " Width: ".$image_size[0];
  60. echo " Length: ".$image_size[1];
  61. echo "
    Size: ".$_FILES['upfile']['size']." bytes" ;
  62. }
  63. echo "
    Image preview:
    ";
  64. echo "Image preview:rFile name: ";
  65. echo "
    ";
  66. }
  67. }
  68. ?>
  69. < input type="submit" name="submit" id="submit" value="button"/>
Copy code

Instructions: When uploading, you need to upload two, otherwise an error will be reported. The code is not very complete, it just gives an idea for learning reference only.



Statement:
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