Experts teach you how to upload multiple images in PHP

Source: Internet
Author: User

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

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.