搜尋
首頁php教程php手册php文件上传代码(支持文件批量上传)

本款文件上传类,默认是上传单文件的,我们只要修改$inputname ='files'为你的表单名就可以方便的实现批量文件上传了。 $savename = ''保存文件名, $alowexts = array()设置允许上传的类型,$savepath = ''保存路径。
*/

 代码如下 复制代码

class upload
{
 public $savepath;
 public $files;
 private $error;

 function __construct($inputname ='files', $savepath = '', $savename = '', $alowexts = array(),$maxsize = 1024000)
 {
  if(!$alowexts)$alowexts=explode('|',upload_ftype);
  $file_array=array();
  $savepath=str_replace('','/',$savepath);
  $savename=preg_replace('/[^a-z0-9_]+/i','',$savename);
  $this->savepath=substr($savepath,-1)=='/'?$savepath:$savepath.'/'; //路径名以/结尾

  if(!make_dir($this->savepath))
  {
   $this->error=8;
   $this->error();
  }
  //exit($this->savepath);
  if(!is_writeable($this->savepath))
  {
   $this->error=9;
   $this->error();
  }
  if(sizeof($_files[$inputname]['error'])>10)
  {
   $this->error=13;
   $this->error();
  }
  $max=sizeof($_files[$inputname]['error'])-1;
  //exit($this->savepath.$savename);
  foreach($_files[$inputname]['error'] as $key => $error)
  {
   if($error==upload_err_ok) //批量上传
   {
    $savename=$savename?$savename:date('ymdims').mt_rand(10000,99999);
    $fileext=strtolower(get_fileext($_files[$inputname]['name'][$key]));
    $savename=$savename.'.'.$fileext;
    $tmp_name=$_files[$inputname]['tmp_name'][$key];
    $filesize=$_files[$inputname]['size'][$key];
    if(!in_array($fileext,$alowexts))
    {
     $this->error=10;
     $this->error();
    }
    if($filesize>$maxsize)
    {
     $this->error=11;
     $this->error();
    }
    if(!$this->isuploadedfile($tmp_name))
    {
     $this->error=12;
     $this->error();
    }

    if(move_uploaded_file($tmp_name,$this->savepath.$savename) || @copy($tmp_name,$this->savepath.$savename))
    {
     //exit($this->savepath.$savename);
     @chmod($savename, 0644);
     @unlink($tmp_name);
     $file_array[]=$this->savepath.$savename;     
    }
   }
   else
   {
    $this->error=$error;
    $this->error();
   }
   unset($savename);
  }
  $this->files=$file_array;
  return true;
 }

 function isuploadedfile($file) //去掉系统自带的反斜线
 {
  return (is_uploaded_file($file) || is_uploaded_file(str_replace('','',$file)));
 }

 function error()
 {
  $upload_error=array(0 => '文件上传成功 !',
       1 => '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值 !',
       2 => '上传文件的大小超过了 html 表单中 max_file_size 选项指定的值 !',
       3 => '文件只有部分被上传 !',
       4 => '没有文件被上传 !',
       5 => '未知错误!',
       6 => '找不到临时文件夹。 !',
       7 => '文件写入临时文件夹失败 !',
       8 => '附件目录创建失败 !',
       9 => '附件目录没有写入权限 !',
       10 => '不允许上传该类型文件 !',
       11 => '文件超过了管理员限定的大小 !',
       12 => '非法上传文件 !',
       13 => '最多可同时上传10个文件 !'
       );
  showmsg($upload_error[$this->error]);
 }

}

//使用方法

new upload();



永久地址:

转载随意~请带上教程地址吧^^

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
1 個月前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳圖形設置
1 個月前By尊渡假赌尊渡假赌尊渡假赌
威爾R.E.P.O.有交叉遊戲嗎?
1 個月前By尊渡假赌尊渡假赌尊渡假赌

熱工具

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

Dreamweaver Mac版

Dreamweaver Mac版

視覺化網頁開發工具