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

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

Jun 21, 2016 am 08:57 AM

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

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

/*********************************************
* 文件: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

       ";



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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)