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:
- <?Php
-
- // Image directory
- $Img_dir="../Upload /";
- //...... Html display upload interface
-
- /* Image Upload Processing */
- // Upload the image to the server
- // Initialize the variable
-
- $Uploaded=0;
- $Unuploaded=0;
-
- // Only five images can be uploaded
-
- For ($I=0; $ I<= 5; $ I ++)
- {
- // Obtain the information of the current image
- $Is_file= $ _ FILES ['imgfile'] ['name'] [$ I];
- // If the current image is not empty
- If (! Empty ($ is_file ))
- {
- // Store the information of the current image in the Variable
- $ Result [$ I] ="
- <TrclassTrclass=Td2align= Center>
- <Td>". $ _ FILES ['imgfile'] ['name'] [$ I]."</Td>
- <Td>". Round ($ _ FILES ['imgfile'] ['SIZE'] [$ I]/1024,2)." K</Td>
- <Td>". $ _ FILES ['imgfile'] ['type'] [$ I]."</Td>
- <Td>";
-
- // Determine whether the type of the uploaded image is jpg, gif, png, or bmp, and whether the upload is successful.
- If (
-
- $ _ FILES ['imgfile'] ['type'] [$ I] = "image/pjpeg" |
- $ _ FILES ['imgfile'] ['type'] [$ I] = "image/gif" |
- $ _ FILES ['imgfile'] ['type'] [$ I] = "image/x-png" |
- $ _ FILES ['imgfile'] ['type'] [$ I] = "image/bmp"
- )
- {
- // If the uploaded file does not exist on the server
- If (! File_exists ($ img_dir. $ _ FILES ['imgfile'] ['name'] [$ I])
- {
- // Transfer the image file from the temporary folder to the directory we specified for upload
- Move_uploaded_file ($ _ FILES ['imgfile'] ['tmp _ name'] [$ I],
- $ Img_dir. $ _ FILES ['imgfile'] ['name'] [$ I]);
- $ Result [$ I].="Successful";
- $ Uploaded ++;
- }
- Else // if the file already exists on the server
- {
- $ Result [$ I].="<Fontcolor = red> the file already exists </font>";
- $ Unuploaded ++;
- Continue;
- }
- }
- Else
- {
- $ Result [$ I].="<Fontcolor = red> failed </font>";
- $ Unuploaded ++;
- }
- $ Result [$ I].="</Td> </tr>";
- } // Endif
- } // Endfor
-
-
- // If no image is selected
- If (empty ($ result ))
- {
- Prompt_msg ("error message", "no image is selected. "," Back to the previous step "," uploadimg. php?Action=Upload");
- Exit ();
- }
-
- // Display all uploaded results
- Echo"<TablecellpaddingTablecellpadding=4 cellspacing=1 border=0 class=Tablewidth=400 align= Left>
- <TrclassTrclass=Navialign= Center>
- <Td>File Name</Td>
- <Td>Size</Td>
- <Td>Type</Td>
- <Td>Upload result</Td>
- </Tr>
- ";
-
- Foreach ($ resultas $ value)
- {
- Echo $ value;
- }
-
- Echo"<TrclassTrclass=Td1>
- <TdcolspanTdcolspan=4>Upload ". ($ uploaded + $ unuploaded).", successful: $ uploaded, failed:<FontcolorFontcolor=Red>$ Unuploaded</Font></Td>
- </Tr>
- <TrclassTrclass=Navi>
- <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>
- </Tr>
- </Table>
- ";
- ?>