search
Homephp教程php手册PHP上传多个图片并校验的代码

PHP上传多个图片并校验的代码

Jun 13, 2016 am 09:33 AM
phpuploadcodeandpictureMultiplerightyescheckFormatof

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

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

  以下为引用的内容:

  // 图片目录

  $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 //如果文件已经在服务器上存在

  {

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

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!