Home > php教程 > php手册 > PHP中多张图片上传并校验的实现

PHP中多张图片上传并校验的实现

WBOY
Release: 2016-06-21 08:59:12
Original
1068 people have browsed it

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

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

/*********************************************
 *  文件:uploadimg.php
 *  用途:图片上传程序
 *  版本:v1.0
 *  创建:2005-03-28 11:07
 *  修改:2005-04-06 08:43
 *  版权:heiyeluren
 *********************************************/

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

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

$uploaded = 0;
$unuploaded = 0;

//只允许五张图片上传

for ($i=0; $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] .= "

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

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

// 显示所有上传后的结果
echo "


      
         
         
         
         
      
       ";

foreach( $result as $value)
{
       echo $value;
}

echo "


     
     
     
      
    
  
共上传 " . ($uploaded + $unuploaded) . ", 成功: $uploaded, 失败: $unuploaded

       ";
?>



Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template