国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php文件上傳類,支持單個或者多個文件上傳

2024-09-04 11:44:10
字體:
來源:轉載
供稿:網友

這個文件上傳類可以實現多個文件或單個文件進行上傳了,下面小編來給各位推薦一個不錯的例子,實例代碼如下:

  1. <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"
  2. <html xmlns="http://www.survivalescaperooms.com/1999/xhtml"
  3. <head> 
  4. <meta http-equiv="content-type" content="text/html; charset=gb2312" /> 
  5. <title>無標題文檔</title> 
  6. </head> 
  7.  
  8. <body> 
  9. <?php 
  10. //php文件上傳類(該類支持單個或者多個文件上傳) 
  11.  /** 
  12.  * 類名:upfile 
  13.  * 作用:處理文件上傳 
  14.  * 說明,該類處理單個或者多個文件上傳,使用該類時,只需要實列化該類 
  15.  * 例: 
  16.  * $up = upfile() 
  17.  * $up->update_file($_file['filename']) 
  18.  * 
  19.  * $up->update_file   函數返回一個數組,如果是多文件上傳,則為多維數據。 
  20.  * 數組的內容: 
  21.  * $fileinfo['file_size']   上傳文件的大小 
  22.  * $fileinfo['file_suffix'] 上傳文件的類型 
  23.  * $fileinfo['file_name']   上傳文件的名字 
  24.  * $fileinfo['error']     上傳文件產生的錯誤 
  25.  * 
  26.  
  27.  */ 
  28. class upfile { 
  29.  public $fcount = 1;           //上傳文件的數量 
  30.  public $ftype  = array('jpg','jpeg','gif','png');  //文件格式 
  31.  public $fsize  = 1024;          //文件大小單位kb 
  32.  public $fdir   = 'www.111cn.net/';         //文件存放目錄 
  33.  public $errormsg = '';          //產生的臨時錯誤信息 
  34.  
  35.  /** 
  36.   *函數名:get_tmp_file($putfile) 
  37.   *作用:取得上傳的臨時文件名 
  38.   *@param array $putfile 
  39.   *@return string $upimg 返回臨時文件名 
  40.   */ 
  41.   function get_tmp_file($putfile){ 
  42.   if($this->fcount == 1){ 
  43.    $tmpfile = $putfile['tmp_name']; 
  44.   }else
  45.    for($i=0;$i<$this->fcount;$i++){ 
  46.     $tmpfile[] = $putfile['tmp_name'][$i]; 
  47.    } 
  48.   } 
  49.   return $tmpfile
  50.   } 
  51.  
  52.  /** 
  53.   *函數名:get_suffix($filename) 
  54.   *作用:取得文件的后綴名 
  55.   *@param file $filename 
  56.   *@return string $suffixname 返回后綴名 
  57.   */ 
  58.   function get_suffix($filename){ 
  59.   $link = pathinfo($filename); 
  60.      $suffixname = strtolower($link['extension']); 
  61.      return $suffixname
  62.   } 
  63.  
  64.  /** 
  65.   *驗證文件大小 
  66.   *@author 趙紅健 
  67.   *@param $filesize 
  68.   *@return booln 
  69.   */ 
  70.  function check_file_size($filesize){ 
  71.   $this->errormsg = ''
  72.   if($filesize/1000 > $this->fsize){ 
  73.    $this->errormsg = '警告:文件超出大小!'
  74.    return false; 
  75.   }else
  76.    return true; 
  77.   } 
  78.  } 
  79.  
  80.  /** 
  81.   *驗證文件類型是否合法 
  82.   *@author 趙紅健 
  83.   *@param $filesuffix 
  84.   *@return booln 
  85.   */ 
  86.  function check_file_suffix($filesuffix){ 
  87.    $this->errormsg = ''
  88.    if(!in_array($filesuffix,$this->ftype)){ 
  89.     $this->errormsg = '警告:文件類型不在允許范圍內!'
  90.     return false; 
  91.    }else
  92.     return true; 
  93.    } 
  94.  } 
  95.  
  96.  /** 
  97.   *移動臨時文件 
  98.   *@author 趙紅健 
  99.   *@param $filesuffix 
  100.   *@return booln 
  101.   */ 
  102.  function move_temp_file($tmpfile,$targetfile){ 
  103.    $this->errormsg = ''
  104.    if(!move_uploaded_file($tmpfile,$targetfile)){ 
  105.     $this->errormsg = '警告:文件移動失??!'
  106.     return false; 
  107.    }else
  108.     return true; 
  109.    } 
  110.  } 
  111.  
  112.  
  113.      /** 
  114.    *函數名:update_file($putfile) 
  115.    *作用:上傳文件 
  116.    *@param array $putfile 
  117.    *@return array 文件信息 
  118.    */ 
  119.     function update_file($putfile){ 
  120.    $tmpfile = $this->get_tmp_file($putfile); 
  121.    if(!file_exists($this->fdir)){ 
  122.        $this->errormsg[] = '錯誤:目錄'.$this->fdir.'不存在'
  123.     return $this->errormsg; 
  124.    } 
  125.    $this->fdir = substr($this->fdir,strlen($this->fdir)-1,1)=='/'?$this->fdir:$this->fdir.'/'
  126.    if(!is_array($putfile['size'])){ 
  127.     $fileinfo['file_size'] = $putfile['size']; 
  128.     if(!$this->check_file_size($fileinfo['file_size'])){ 
  129.      $fileinfo['error'] = $this->errormsg; 
  130.      return $fileinfo
  131.     } 
  132.     $fileinfo['file_suffix'] = $this->get_suffix($putfile['name']); 
  133.     if(!$this->check_file_suffix($fileinfo['file_suffix'])){ 
  134.      $fileinfo['error'] = $this->errormsg; 
  135.      return $fileinfo
  136.     } 
  137.  
  138.     $fileinfo['file_name']   = date('ymdhms').'.'.$fileinfo['file_suffix']; 
  139.     if(!$this->move_temp_file($tmpfile,$this->fdir.$fileinfo['file_name'])){ 
  140.      $fileinfo['error'] = $this->errormsg; 
  141.      return $fileinfo
  142.     } 
  143.     return $fileinfo
  144.  
  145.    }else
  146.     for($i=0;$i<$this->fcount;$i++){ 
  147.      $fileinfo[$i]['file_size'] = $putfile['size'][$i]; 
  148.      if(!$this->check_file_size($fileinfo[$i]['file_size'])){ 
  149.       $fileinfo[$i]['error'] = $this->errormsg; 
  150.       continue
  151.      } 
  152.  
  153.      $fileinfo[$i]['file_suffix'] = $this->get_suffix($putfile['name'][$i]); 
  154.      if(!$this->check_file_suffix($fileinfo[$i]['file_suffix'])){ 
  155.       $fileinfo[$i]['error'] = $this->errormsg; 
  156.       continue
  157.      } 
  158.  
  159.      $fileinfo[$i]['file_name']  = date('ymdhms').rand().'.'.$fileinfo[$i]['file_suffix']; 
  160.      if(!$this->move_temp_file($tmpfile[$i],$this->fdir.$fileinfo[$i]['file_name'])){ 
  161.       $fileinfo[$i]['error'] = $this->errormsg; 
  162.       continue;//開源代碼Vevb.com 
  163.      } 
  164.      } 
  165.     return $fileinfo
  166.    } 
  167.     } 
  168.  
  169. ?> 
  170. </body> 
  171. </html> 

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 三门县| 博爱县| 策勒县| 孙吴县| 巴彦淖尔市| 蒙阴县| 仁寿县| 襄樊市| 云南省| 万载县| 印江| 民县| 广水市| 株洲市| 天峨县| 盐源县| 东源县| 奎屯市| 临江市| 南投县| 乐都县| 萍乡市| 镇原县| 龙州县| 姜堰市| 天祝| 遂平县| 海淀区| 锦州市| 永顺县| 深泽县| 鸡泽县| 临城县| 镇原县| 万宁市| 德州市| 电白县| 固镇县| 宁都县| 资兴市| 黎平县|