Home  >  Article  >  Backend Development  >  Implementation of uploading and verifying multiple images in PHP_PHP Tutorial

Implementation of uploading and verifying multiple images in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:37:56943browse

Uploading a single image is not complicated. This involves uploading multiple images and verifying the image format to ensure that the uploaded image must be an image and 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.

/*********************************************
* File: uploadimg.php
* Purpose: Image upload program
* Version: v1.0
* Created: 2005-03-28 11:07
* Modified: 2005-04-06 08:43
*Copyright: heiyeluren
**********************************************/

// Picture directory
$img_dir = "../upload/";
// ...... html display upload interface

/* Image upload processing */
//Transmit 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 information of the current picture into 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 picture is one of jpg, gif, png, 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]))
                                                             {
//Move 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";
                                                                         }
                                                                                                                                                                                                                         //If the file already exists on the server
                                                                {
$result[$i] .= "File already exists";
                                                                                                                               continue;                     }
              }
              else
                   {
                                         $result[$i] .= "Failed";
                        $unuploaded++;
              }
$result[$i] .= "";
           } //end if
} // end for


// If no image is selected

if (empty($result))

{
​​​ prompt_msg("Error message", "No images selected.", "Return to previous step", "uploadimg.php?action=upload" );
exit();
}

// Display all uploaded results
echo "


         
                                                                                                                                                                                                                                                                                                                                                                                                                                                         


http://www.bkjia.com/PHPjc/486514.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486514.htmlTechArticleUploading a single image is not complicated. This involves uploading multiple images and verifying the image format. , to ensure that images must be uploaded and to prevent other files from being uploaded to the server. Basic reality...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn