Home  >  Article  >  Backend Development  >  PHP example: Upload multiple images and verify the code_PHP tutorial

PHP example: Upload multiple images and verify the code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:29:33840browse

单张的图片上传是不复杂的,这里涉及到多张图片上传和对图片格式的校验,保证上传的一定是图片,防止上传其他文件到服务器。

基本实现算法是使用数组的形式,把所有的图片提交个一个数组,对数组的元素进行一个个的处理。


                           
                           
                           
                            ";
       } //end if
} // end for
 

// 如果没有选择任何图片
if (empty($result))
{
       prompt_msg("错误信息", "没有选择任何图片。", "返回上一步", "uploadimg.php?action=upload" );
       exit();
}

// Display all uploaded results
echo "

以下为引用的内容:

// 图片目录
$img_dir = "../upload/";
// …… html 显示上传界面

/* 图片上传处理 */
// 把图片传到服务器
// 初始化变量

$uploaded = 0;
$unuploaded = 0;

//只允许五张图片上传

for ($i=0; $i<=5; $i++)
{
//获取当前图片的信息
$is_file = $_FILES[imgfile][name][$i];
//如果当前图片不为空
if (!empty($is_file))
{
//把当前图片的信息存储到变量里
$result[$i] = "

". $_FILES[imgfile][name][$i] ."". round($_FILES[imgfile][size][$i]/1024, 2) ."K". $_FILES[imgfile][type][$i] ."";

              // 判断上传的图片的类型是不是jpg,gif,png,bmp中的一种,同时判断是否上传成功
              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 (!file_exists($img_dir . $_FILES[imgfile][name][$i]))
                     {
                            //把图片文件从临时文件夹中转移到我们指定上传的目录中
                            move_uploaded_file($_FILES[imgfile][tmp_name][$i],
      $img_dir . $_FILES[imgfile][name][$i]);
                            $result[$i] .= "成功";
                            $uploaded++;
                     }
                     else         //如果文件已经在服务器上存在
                     {
                            $result[$i] .= "文件已存在";
                            $unuploaded++;
                            continue;
                     }
              }
              else
              {
                     $result[$i] .= "失败";
                     $unuploaded++;
              }
              $result[$i] .= "


                                                                                                                      ;
                                                                                    lt;td>Upload results

“;

foreach( $result as $value)
{

echo $value;

}

echo "



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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531674.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