PHP sample code for uploading multiple files and images

Source: Internet
Author: User
Tags php sample code
PHP sample code for uploading multiple files and images

  1. $ Uptypes = array (
  2. // The ContentType format of the uploaded file
  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.doc ument', // 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 and multi-image Upload
  18. * By bbs.it-home.org
  19. */
  20. $ Max_file_size = 2000000; // size limit of uploaded files, in bytes
  21. $ Dir = "upload/"; // file upload 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]) // check whether a file exists
  28. {
  29. Echo "the image 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 large! ";
  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 "a file with the same name already exists ";
  49. Exit;
  50. }
  51. If (! Move_uploaded_file ($ filename, $ destination ))
  52. {
  53. Echo "an error occurred while moving the file ";
  54. Exit;
  55. }
  56. $ Pinfo = pathinfo ($ destination );
  57. $ Fname = $ pinfo ['basename'];
  58. Echo "uploaded successfully
    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 "echo" alt = \ "image preview: \ r File name:". $ destination. "\ r Upload Time: \"> ";
  65. Echo"
    ";
  66. }
  67. }
  68. ?>

Note: You need to upload two files during Upload. Otherwise, an error is reported. The code is not perfect. it just provides an idea for your reference.

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.