Uploading a single image is not complex. This involves uploading multiple images and verifying the image format. This ensures that the uploaded images are always used to prevent uploading other files 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
/*************************************** ******
* File: uploadimg. php
* Purpose: upload an image program
* Version: v1.0
* Created:
* Modification:
* Copyright: heiyeluren
**************************************** *****/
// 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] ="
<Tr class = td2 align = 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]. = "<font color = red> the file already exists </font> ";
$ Unuploaded ++;
Continue;
}
}
Else
{
$ Result [$ I]. = "<font color = red> failed </font> ";
$ Unuploaded ++;
}
$ Result [$ I]. = "</td> </tr> ";
} // End if
} // End
// 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 "<table cellpadding = 4 cellspacing = 1 border = 0 class = table width = 400 align = left>
<Tr class = navi align = center>
<Td> file name </td>
<Td> size </td>
<Td> type </td>