How to upload and verify multiple images in PHP

Source: Internet
Author: User
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 an array to submit all images to an array and process the elements of the array one by one .? Php/************************** "> <LINKhref =" http: // w

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.

/*************************************** ******
* 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] ="

". $ _ 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 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]. = "the file already exists ";
$ Unuploaded ++;
Continue;
}
}
Else
{
$ Result [$ I]. = "failed ";
$ Unuploaded ++;
}
$ Result [$ I]. ="";
} // 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"








Foreach ($ result as $ value)
{
Echo $ value;
}

Echo"







";
File name Size Type Upload result
Upload ". ($ uploaded + $ unuploaded).", successful: $ uploaded, failed: $ unuploaded
[Continue uploading] [browsing Images]

";
 

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.