最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

php文件上传代码(支持文件批量上传)

时间:2014-06-09 16:40来源: 作者: 点击: 次
本款文件上传类,默认是上传单文件的,我们只要修改$inputname =files为你的表单名就可以方便的实现批量文件上传了,$savename = 保存文件名, $alowexts = array()设置允许上传的类型,$savepath = 保存路

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

  1. */ 
  2. class upload 
  3. { 
  4.  public $savepath; 
  5.  public $files; 
  6.  private $error; 
  7.  function __construct($inputname ='files', $savepath = '', $savename = '', $alowexts = array(),$maxsize = 1024000) 
  8.  { 
  9.   if(!$alowexts)$alowexts=explode('|',upload_ftype); 
  10.   $file_array=array(); 
  11.   $savepath=str_replace('','/',$savepath); 
  12.   $savename=preg_replace('/[^a-z0-9_]+/i','',$savename); 
  13.   $this->savepath=substr($savepath,-1)=='/'?$savepath:$savepath.'/'; //路径名以/结尾 
  14.   if(!make_dir($this->savepath)) 
  15.   { 
  16.    $this->error=8; 
  17.    $this->error(); 
  18.   } 
  19.   //exit($this->savepath); 
  20.   if(!is_writeable($this->savepath)) 
  21.   { 
  22.    $this->error=9; 
  23.    $this->error(); 
  24.   } 
  25.   if(sizeof($_files[$inputname]['error'])>10) 
  26.   { 
  27.    $this->error=13; 
  28.    $this->error(); 
  29.   } 
  30.   $max=sizeof($_files[$inputname]['error'])-1; 
  31.   //exit($this->savepath.$savename); 
  32.   foreach($_files[$inputname]['error'] as $key => $error) 
  33.   { 
  34.    if($error==upload_err_ok) //批量上传 
  35.    { 
  36.     $savename=$savename?$savename:date('ymdims').mt_rand(10000,99999);  
  37.     $fileext=strtolower(get_fileext($_files[$inputname]['name'][$key])); 
  38.     $savename=$savename.'.'.$fileext; 
  39.     $tmp_name=$_files[$inputname]['tmp_name'][$key]; 
  40.     $filesize=$_files[$inputname]['size'][$key]; 
  41.     if(!in_array($fileext,$alowexts)) 
  42.     { 
  43.      $this->error=10; 
  44.      $this->error(); 
  45.     } 
  46.     if($filesize>$maxsize) 
  47.     { 
  48.      $this->error=11; 
  49.      $this->error(); 
  50.     } 
  51.     if(!$this->isuploadedfile($tmp_name)) 
  52.     { 
  53.      $this->error=12; 
  54.      $this->error(); 
  55.     } 
  56.     if(move_uploaded_file($tmp_name,$this->savepath.$savename) || @copy($tmp_name,$this->savepath.$savename)) 
  57.     { 
  58.      //exit($this->savepath.$savename); 
  59.      @chmod($savename, 0644); 
  60.      @unlink($tmp_name); 
  61.      $file_array[]=$this->savepath.$savename;      
  62.     } 
  63.    } 
  64.    else 
  65.    { 
  66.     $this->error=$error; 
  67.     $this->error(); 
  68.    } 
  69.    unset($savename); 
  70.   } 
  71.   $this->files=$file_array; 
  72.   return true; 
  73.  } 
  74.  function isuploadedfile($file) //去掉系统自带的反斜线 
  75.  { 
  76.   return (is_uploaded_file($file) || is_uploaded_file(str_replace('\','',$file)));  
  77.  } 
  78.  function error() 
  79.  { 
  80.   $upload_error=array(0 => '文件上传成功 !', 
  81.        1 => '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值 !', 
  82.        2 => '上传文件的大小超过了 html 表单中 max_file_size 选项指定的值 !', 
  83.        3 => '文件只有部分被上传 !', 
  84.        4 => '没有文件被上传 !', 
  85.        5 => '未知错误!', 
  86.        6 => '找不到临时文件夹。 !', 
  87.        7 => '文件写入临时文件夹失败 !', 
  88.        8 => '附件目录创建失败 !', 
  89.        9 => '附件目录没有写入权限 !', 
  90.        10 => '不允许上传该类型文件 !', 
  91.        11 => '文件超过了管理员限定的大小 !', 
  92.        12 => '非法上传文件 !', 
  93.        13 => '最多可同时上传10个文件 !' 
  94.        ); 
  95.   showmsg($upload_error[$this->error]); 
  96.  } 
  97. } 
  98. //使用方法 
  99. new upload(); 
(责任编辑:admin)
------分隔线----------------------------
栏目列表
推荐内容