How to upload multiple images in PHP _ PHP Tutorial

Source: Internet
Author: User
Experts teach you how to upload multiple images in PHP. When learning PHP, you may encounter the problem of uploading multiple images in PHP. here we will introduce the solution to the problem of uploading multiple images in PHP. here we will share it with you. When learning PHP on a single image, you may encounter the problem of uploading multiple images in PHP. here we will introduce the solution to the problem of uploading multiple images in PHP. I will share it with you here. Uploading a single image is not complex. this involves uploading multiple images in PHP and verifying the image format. This ensures that images are uploaded and other files are uploaded to the server.

The basic implementation algorithm uses arrays to submit an array of all images and process the elements of the array one by one. PHP is actually a module function of the Web server. Therefore, you must first ensure the security of the Web server. Of course, to ensure the security of Web servers, you must first ensure the system security. this is a long way to go and is endless. PHP can be combined with various Web servers. here we only discuss Apache. We strongly recommend that you install and start Apache in the form of chroot. in this way, even if Apache, PHP, and their scripts are prone to vulnerabilities, only the banned system will be affected and the actual system will not be harmed. Reference content is as follows:

 
 
  1. Php
  2. // Image directory
  3. $Img_dir="../Upload /";
  4. //...... Html display Upload interface
  5. /* Image Upload processing */
  6. // Upload the image to the server
  7. // Initialize the variable
  8. $Uploaded=0;
  9. $Unuploaded=0;
  10. // Only five images can be uploaded
  11. For ($I=0; $ I<= 5; $ I ++)
  12. {
  13. // Obtain the information of the current image
  14. $Is_file= $ _ FILES ['imgfile'] ['name'] [$ I];
  15. // If the current image is not empty
  16. If (! Empty ($ is_file ))
  17. {
  18. // Store the information of the current image in the variable
  19. $ Result [$ I] ="
  20. <TrclassTrclass=Td2align= Center>
  21. <Td>". $ _ FILES ['imgfile'] ['name'] [$ I]." Td>
  22. <Td>". Round ($ _ FILES ['imgfile'] ['size'] [$ I]/1024,2)." K Td>
  23. <Td>". $ _ FILES ['imgfile'] ['type'] [$ I]." Td>
  24. <Td>";
  25. // Determine whether the type of the uploaded image is jpg, gif, png, or bmp, and whether the upload is successful.
  26. If (
  27. $ _ FILES ['imgfile'] ['type'] [$ I] = "image/pjpeg" |
  28. $ _ FILES ['imgfile'] ['type'] [$ I] = "image/gif" |
  29. $ _ FILES ['imgfile'] ['type'] [$ I] = "image/x-png" |
  30. $ _ FILES ['imgfile'] ['type'] [$ I] = "image/bmp"
  31. )
  32. {
  33. // If the uploaded file does not exist on the server
  34. If (! File_exists ($ img_dir. $ _ FILES ['imgfile'] ['name'] [$ I])
  35. {
  36. // Transfer the image file from the temporary folder to the directory we specified for Upload
  37. Move_uploaded_file ($ _ FILES ['imgfile'] ['tmp _ name'] [$ I],
  38. $ Img_dir. $ _ FILES ['imgfile'] ['name'] [$ I]);
  39. $ Result [$ I].="Successful";
  40. $ Uploaded ++;
  41. }
  42. Else // if the file already exists on the server
  43. {
  44. $ Result [$ I].="File already exists";
  45. $ Unuploaded ++;
  46. Continue;
  47. }
  48. }
  49. Else
  50. {
  51. $ Result [$ I].="Failed";
  52. $ Unuploaded ++;
  53. }
  54. $ Result [$ I].="";
  55. } // Endif
  56. } // Endfor
  57. // If no image is selected
  58. If (empty ($ result ))
  59. {
  60. Prompt_msg ("error message", "No image is selected. "," Back to the previous step "," uploadimg. php?Action=Upload");
  61. Exit ();
  62. }
  63. // Display all uploaded results
  64. Echo"<TablecellpaddingTablecellpadding=4 cellspacing=1 border=0 class=Tablewidth=400 align= Left>
  65. <TrclassTrclass=Navialign= Center>
  66. <Td>File name Td>
  67. <Td>Size Td>
  68. <Td>Type Td>
  69. <Td>Upload result Td>
  70. Tr>
  71. ";
  72. Foreach ($ resultas $ value)
  73. {
  74. Echo $ value;
  75. }
  76. Echo"<TrclassTrclass=Td1>
  77. <TdcolspanTdcolspan=4>Upload ". ($ uploaded + $ unuploaded).", successful: $ uploaded, failed:<FontcolorFontcolor=Red>$ Unuploaded Font> Td>
  78. Tr>
  79. <TrclassTrclass=Navi>
  80. <TdcolspanTdcolspan=4 align= Center>[<AhrefAhref= 'Uploadimg. php? Action = upload'Title= 'Continue upload'>Continue Upload A>]
    [<AhrefAhref= 'Image. php'Title= 'Browse image'>Browse images A>] Td>
  81. Tr>
  82. Table>
  83. ";
  84. ?>

Bytes. Single picture...

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.