Home > Article > Backend Development > PHP code to upload multiple images and verify them_PHP tutorial
Uploading a single image is not complicated. This involves uploading multiple images and verifying the image format to ensure that only images are uploaded and to prevent other files from being uploaded to the server.
The basic implementation algorithm is to use the form of an array, submit all the pictures into an array, and process the elements of the array one by one.
The following is the quoted content:
// Picture directory
$img_dir = "../upload/";
// …… html display upload interface
/* Image upload processing */
// Transfer the image to the server
// Initialize variables
$uploaded = 0;
$unuploaded = 0;
//Only five pictures are allowed to be uploaded
for ($i=0; $i<=5; $i++)
{
//Get information about the current picture
$is_file = $_FILES['imgfile']['name'][$i];
//If the current picture is not empty
if (!empty($is_file))
{
// Store the current picture information into variables
$result[$i] = "
".$_FILES['imgfile']['name'][$i] ."
". round($_FILES['imgfile']['size'][$i]/1024, 2) ."K
".$_FILES['imgfile']['type'][$i] ."
";
// Determine whether the type of the uploaded image is one of jpg, gif, png, and bmp, and determine 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 files from the temporary folder to the directory we specify for uploading
move_uploaded_file($_FILES['imgfile']['tmp_name'][$i],
$img_dir . $_FILES['imgfile']['name'][$i]);
$result[$i] .= "Success";
$uploaded++;
}
else //If the file already exists on the server
{