PHP Upload multiple pictures and verify the code

Source: Internet
Author: User

Single picture upload is not complicated, here involves many pictures upload and the picture format of the check, to ensure that the upload must be pictures, to prevent uploading other files to the server.

The basic implementation algorithm is to use the form of an array, all the pictures are submitted to an array, the elements of the array to deal with each one.

The following are the referenced contents:

Picture Catalogue

$img _dir = ". /upload/";

... HTML display upload interface

/* Image upload processing * *

Upload pictures to the server

Initializing variables

$uploaded = 0;

$unuploaded = 0;

Only allow five pictures to upload

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 information in the current picture in a variable

$result [$i] = "

". $_files[' imgfile ' [' name '] [$i]. "

". Round ($_files[' imgfile '] [' size '] [$i]/1024, 2]. " K

". $_files[' imgfile ' [' type '] [$i]. "

";

Determine if the type of uploaded picture is one of the jpg,gif,png,bmp and whether the upload succeeds

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 picture file from the temporary folder to the directory we specified to upload

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

{

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.