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 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>

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.