php专区

 首页 > php专区 > PHP应用 > php类库 > php文件上传类,支持单个或者多个文件上传 - php类

php文件上传类,支持单个或者多个文件上传 - php类

分享到:
【字体:
导读:
          这个文件上传类可以实现多个文件或单个文件进行上传了,下面小编来给各位推荐一个不错的例子,实例代码如下:!doctypehtmlpublic-//w3c//dtdxhtml1.0transitional//enhttp://www.w3.org/tr/xhtml1......

php文件上传类,支持单个或者多个文件上传

这个文件上传类可以实现多个文件或单个文件进行上传了,下面小编来给各位推荐一个不错的例子,实例代码如下:

  1. public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"
  2. "http://www.phpfensi.com/1999/xhtml"
  3.  
  4. "content-type" content="text/html; charset=gb2312" /> 
  5. 无标题文档 
  6.  
  7.  
  8.  
  9. //php文件上传类(该类支持单个或者多个文件上传) 
  10.  /** 
  11.  * 类名:upfile 
  12.  * 作用:处理文件上传 
  13.  * 说明,该类处理单个或者多个文件上传,使用该类时,只需要实列化该类 
  14.  * 例: 
  15.  * $up = upfile() 
  16.  * $up->update_file($_file['filename']) 
  17.  * 
  18.  * $up->update_file   函数返回一个数组,如果是多文件上传,则为多维数据。 
  19.  * 数组的内容: 
  20.  * $fileinfo['file_size']   上传文件的大小 
  21.  * $fileinfo['file_suffix'] 上传文件的类型 
  22.  * $fileinfo['file_name']   上传文件的名字 
  23.  * $fileinfo['error']     上传文件产生的错误 
  24.  * 
  25.  
  26.  */ 
  27. class upfile { 
  28.  public $fcount = 1;           //上传文件的数量 
  29.  public $ftype  = array('jpg','jpeg','gif','png');  //文件格式 
  30.  public $fsize  = 1024;          //文件大小单位kb 
  31.  public $fdir   = 'www.111cn.net/';         //文件存放目录 
  32.  public $errormsg = '';          //产生的临时错误信息 
  33.  
  34.  /** 
  35.   *函数名:get_tmp_file($putfile) 
  36.   *作用:取得上传的临时文件名 
  37.   *@param array $putfile 
  38.   *@return string $upimg 返回临时文件名 
  39.   */ 
  40.   function get_tmp_file($putfile){ 
  41.   if($this->fcount == 1){ 
  42.    $tmpfile = $putfile['tmp_name']; 
  43.   }else
  44.    for($i=0;$i<$this->fcount;$i++){ 
  45.     $tmpfile[] = $putfile['tmp_name'][$i]; 
  46.    } 
  47.   } 
  48.   return $tmpfile
  49.   } 
  50.  
  51.  /** 
  52.   *函数名:get_suffix($filename) 
  53.   *作用:取得文件的后缀名 
  54.   *@param file $filename 
  55.   *@return string $suffixname 返回后缀名 
  56.   */ 
  57.   function get_suffix($filename){ 
  58.   $link = pathinfo($filename); 
  59.      $suffixname = strtolower($link['extension']); 
  60.      return $suffixname
  61.   } 
  62.  
  63.  /** 
  64.   *验证文件大小 
  65.   *@author 赵红健 
  66.   *@param $filesize 
  67.   *@return booln 
  68.   */ 
  69.  function check_file_size($filesize){ 
  70.   $this->errormsg = ''
  71.   if($filesize/1000 > $this->fsize){ 
  72.    $this->errormsg = '警告:文件超出大小!'
  73.    return false; 
  74.   }else
  75.    return true; 
  76.   } 
  77.  } 
  78.  
  79.  /** 
  80.   *验证文件类型是否合法 
  81.   *@author 赵红健 
  82.   *@param $filesuffix 
  83.   *@return booln 
  84.   */ 
  85.  function check_file_suffix($filesuffix){ 
  86.    $this->errormsg = ''
  87.    if(!in_array($filesuffix,$this->ftype)){ 
  88.     $this->errormsg = '警告:文件类型不在允许范围内!'
  89.     return false; 
  90.    }else
  91.     return true; 
  92.    } 
  93.  } 
  94.  
  95.  /** 
  96.   *移动临时文件 
  97.   *@author 赵红健 
  98.   *@param $filesuffix 
  99.   *@return booln 
  100.   */ 
  101.  function move_temp_file($tmpfile,$targetfile){ 
  102.    $this->errormsg = ''
  103.    if(!move_uploaded_file($tmpfile,$targetfile)){ 
  104.     $this->errormsg = '警告:文件移动失败!'
  105.     return false; 
  106.    }else
  107.     return true; 
  108.    } 
  109.  } 
  110.  
  111.  
  112.      /** 
  113.    *函数名:update_file($putfile) 
  114.    *作用:上传文件 
  115.    *@param array $putfile 
  116.    *@return array 文件信息 
  117.    */ 
  118.     function update_file($putfile){ 
  119.    $tmpfile = $this->get_tmp_file($putfile); 
  120.    if(!file_exists($this->fdir)){ 
  121.        $this->errormsg[] = '错误:目录'.$this->fdir.'不存在'
  122.     return $this->errormsg; 
  123.    } 
  124.    $this->fdir = substr($this->fdir,strlen($this->fdir)-1,1)=='/'?$this->fdir:$this->fdir.'/'
  125.    if(!is_array($putfile['size'])){ 
  126.     $fileinfo['file_size'] = $putfile['size']; 
  127.     if(!$this->check_file_size($fileinfo['file_size'])){ 
  128.      $fileinfo['error'] = $this->errormsg; 
  129.      return $fileinfo
  130.     } 
  131.     $fileinfo['file_suffix'] = $this->get_suffix($putfile['name']); 
  132.     if(!$this->check_file_suffix($fileinfo['file_suffix'])){ 
  133.      $fileinfo['error'] = $this->errormsg; 
  134.      return $fileinfo
  135.     } 
  136.  
  137.     $fileinfo['file_name']   = date('ymdhms').'.'.$fileinfo['file_suffix']; 
  138.     if(!$this->move_temp_file($tmpfile,$this->fdir.$fileinfo['file_name'])){ 
  139.      $fileinfo['error'] = $this->errormsg; 
  140.      return $fileinfo
  141.     } 
  142.     return $fileinfo
  143.  
  144.    }else
  145.     for($i=0;$i<$this->fcount;$i++){ 
  146.      $fileinfo[$i]['file_size'] = $putfile['size'][$i]; 
  147.      if(!$this->check_file_size($fileinfo[$i]['file_size'])){ 
  148.       $fileinfo[$i]['error'] = $this->errormsg; 
  149.       continue
  150.      } 
  151.  
  152.      $fileinfo[$i]['file_suffix'] = $this->get_suffix($putfile['name'][$i]); 
  153.      if(!$this->check_file_suffix($fileinfo[$i]['file_suffix'])){ 
  154.       $fileinfo[$i]['error'] = $this->errormsg; 
  155.       continue
  156.      } 
  157.  
  158.      $fileinfo[$i]['file_name']  = date('ymdhms').rand().'.'.$fileinfo[$i]['file_suffix']; 
  159.      if(!$this->move_temp_file($tmpfile[$i],$this->fdir.$fileinfo[$i]['file_name'])){ 
  160.       $fileinfo[$i]['error'] = $this->errormsg; 
  161.       continue;//开源代码phpfensi.com 
  162.      } 
  163.      } 
  164.     return $fileinfo
  165.    } 
  166.     } 
  167.  
  168. ?> 
  169.  
  170.  
分享到:
php _autoload自动加载类与机制分析 - ph...
php _autoload自动加载类与机制分析 在使用PHP的OO模式开发系统时,通常大家习惯上将每个类的实现都存放在一个单独的文件里,这样会很容易实现对类进行复用,同时将来维护时也很便利,这也是OO设计的基本思想之一,在PHP5之前,如果需要使用一个类,只需要直接使用include/require将其包含进来即可. ...
MySQL数据库PHP操作类 - php类库
MySQL数据库PHP操作类 下面是一个站长写的一个非常简单实用的数据库操作类了,这里面主要功能是数据库连接,查询数据,删除数据,更新数据等等,这个类的特点时不限制mysql而是直接执行由外部给出的sql语句了. MySQL数据库PHP操作类,代码如下:
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……