最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

非常經(jīng)典的PHP文件上傳類分享

 更新時(shí)間:2016年05月15日 09:46:05   作者:LoeYueng  
這篇文章主要為大家詳細(xì)介紹了一個(gè)經(jīng)典的PHP文件上傳類,降低功能的編寫難度,也為了能節(jié)省開發(fā)時(shí)間,通常我們都會(huì)將這些反復(fù)使用的一段代碼封裝到一個(gè)類中,本文為大家分享了PHP文件上傳類,需要的朋友可以參考下

文件上傳是項(xiàng)目開發(fā)中比較常見的功能,但文件上傳的過程比較繁瑣,只要是有文件上傳的地方就需要編寫這些復(fù)雜的代碼。為了能在每次開發(fā)中降低功能的編寫難度,也為了能節(jié)省開發(fā)時(shí)間,通常我們都會(huì)將這些反復(fù)使用的一段代碼封裝到一個(gè)類中。

<?php 
/** 
 +----------------------------------------------------------------------------- 
 * 文件上傳類 
 +----------------------------------------------------------------------------- 
 * @author Administrator 
 +----------------------------------------------------------------------------- 
 */ 
 class FileUpload{ 
   
  private $filepath;  //保存路徑 
  private $allowtype=array('gif','jpg','jpeg','png','txt'); 
  private $maxsize=1000000;  //最大允許上傳大小 
  private $israndname=true; //是否隨機(jī) 
  private $orginame; //原始文件名 
  private $tmpname;  //臨時(shí)文件名 
  private $newname;  //新文件名 
  private $filetype; //文件類型  
  private $filesize; //文件大小 
  private $errornum=''; //錯(cuò)誤號(hào) 
  private $errormsg; //錯(cuò)誤信息 
/** 
 +------------------------------------------------------------------------------ 
 *構(gòu)造函數(shù) 
 +------------------------------------------------------------------------------ 
 * @param string $savepath  保存路徑 
 * @param string $allowtype 允許類型 
 * @param string $maxsize  允許大小 
 * 
 +------------------------------------------------------------------------------ 
 */    
  function __construct($option=array()){ 
     foreach ($option as $key=>$value){ 
        
      if (!in_array($key,get_class_vars(get_class($this)))){ 
        continue; 
       } 
      $this->setOption($key, $value);  
     } 
  } 
  function uploadfile($field) { 
     $return=true; 
    if (!$this->CheckPath()) { 
      $this->errormsg=$this->geterrorNum(); 
      return false; 
    } 
    $name=$_FILES[$field]['name']; 
    $tmpname=$_FILES[$field]['tmp_name']; 
    $filesize=$_FILES[$field]['size']; 
    $error=$_FILES[$field]['error'];  
     if (is_array($name)) { 
      $errors=array(); 
       for ($i=0;$i<count($name);$i++){ 
        if ($this->getFile($name[$i],$tmpname[$i],$filesize[$i],$errors[$i])) { 
          if (!$this->CheckSize() && !$this->CheckType()) { 
            $errors=$this->getErrorNum(); 
            return false; 
            }                                 
        }else{ 
            $errors=$this->getErrorNum(); 
            return false; 
        } 
        if (!$return) { 
          $this->getFile(); 
        } 
       } 
       if ($return) { 
        $fileNames=array(); 
        for ($i=0;$i<count($name);$i++){ 
          if ($this->getFile($name[$i], $tmpname[$i], $filesize[$i], $filesize[$i])) { 
            $this->SetFileName(); 
            if (!$this->MoveFile()) { 
              $errors[]=$this->getErrorNum(); 
              $return=false; 
            }else{ 
              $fileNames[]=$this->getNewName(); 
            } 
          }         
        } 
        $this->newname=$fileNames; 
       } 
        $this->errormsg=$errors; 
        return $return; 
        
     }else{  
      if($this->getFile($name,$tmpname,filesize,$error)){ 
        if(!$this->CheckSize()){ 
          return false; 
        } 
        if(!$this->CheckType()){ 
          return false; 
        } 
         $this->SetFileName(); 
        if ($this->MoveFile()) { 
            return true; 
        }  
      }else{ 
      return false; 
      } 
        
      if (!$return) { 
      $this->setOption('ErrorNum', 0); 
      $this->errormsg=$this->geterrorNum(); 
      } 
     return $return;  
   } 
  } 
  /** 
   +------------------------------------------------------------------------ 
   *設(shè)置類屬性值函數(shù) 
   +------------------------------------------------------------------------ 
   * @param mix $key 
   * @param mix $value 
   */ 
  private function setOption($key,$value){ 
    $key=strtolower($key); 
    $this->$key=$value; 
  } 
  /** 
   +--------------------------------------------------------------------------- 
   * 獲取文件變量參數(shù)函數(shù) 
   +--------------------------------------------------------------------------- 
   * @param string $name 
   * @param string $tmp_name 
   * @param number $size 
   * @param number $error 
   */ 
  private function getFile($name,$tmpname,$filetype,$filesize,$error=0){  
     
    $this->setOption('TmpName', $tmpname); 
    $this->setOption('OrgiName', $name); 
    $arrstr=explode('.', $name); 
    $this->setOption('FileType', $arrstr[count($arrstr)-1]);      
    $this->setOption('FileSize', $filesize); 
    return true; 
  } 
  /** 
   +------------------------------------------------------------------------- 
   * 檢查上傳路徑函數(shù) 
   +------------------------------------------------------------------------- 
   * @return boolean 
   */ 
  private function CheckPath(){ 
    if(empty($this->filepath)){ 
      $this->setOption('ErrorNum', -5); 
      return false; 
    } 
    if (!file_exists($this->filepath)||!is_writable($this->filepath)) { 
       if (!@mkdir($this->filepath,0755)) { 
         $this->setOption('ErrorNum',-4); 
         return false; 
       } 
    } 
    return true; 
  } 
  private function Is_Http_Post(){ 
    if (!is_uploaded_file($this->tmpname)) { 
      $this->setOption('ErrorNum',-6);  
      return false; 
    }else{ 
      return true; 
    } 
  } 
  /** 
   +-------------------------------------------------------------------- 
   *檢查文件尺寸函數(shù) 
   +-------------------------------------------------------------------- 
   * @return boolean 
   */ 
  private function CheckSize(){ 
    if ($this->filesize>$this->maxsize) { 
      $this->setOption('ErrorNum', -2); 
      return false; 
    }else{ 
      return true; 
    } 
  } 
  /** 
   +--------------------------------------------------------------- 
   * 檢查文件類型函數(shù) 
   +--------------------------------------------------------------- 
   * @return boolean 
   */ 
  private function CheckType(){ 
    if (in_array($this->filetype, $this->allowtype)) { 
      return true; 
    }else{ 
      $this->setOption('ErrorNum', -1); 
      return false; 
    } 
  } 
  private function SetFileName(){ 
    if ($this->israndname) { 
      $this->setOption('NewName', $this->RandName()); 
    }else{ 
      $this->setOption('NewName',$this->orginame); 
    }  
  } 
  /** 
   +----------------------------------------------------------------- 
   * 獲取新文件名 
   +------------------------------------------------------------------ 
   */ 
  public function getNewName() { 
    return $this->newname; 
  } 
  private function RandName(){ 
    $rule=date("YmdHis").rand(0, 999); 
    return $rule.'.'.$this->filetype; 
  } 
  private function MoveFile(){ 
    if ($this->errornum) { 
      $filepath=rtrim($this->filaepath,'/').'/'; 
      $filepath.=$this->newname; 
      if (@move_uploaded_file($this->tmpname,$filepath)) { 
        return true; 
       }else{ 
        $this->errormsg=$this->setOption('ErrorNum',-3 ); 
       } 
    }else{ 
      return false; 
    } 
  } 
  /** 
   +---------------------------------------------------------------- 
   * 錯(cuò)誤信息函數(shù) 
   +---------------------------------------------------------------- 
   * @return string 
   */ 
   function getErrorNum() { 
    $erstr="上傳文件<font color='red'>{$this->orginame}</font>出錯(cuò)"; 
    switch ($this->errornum) { 
      case 4: 
       $erstr.="沒有文件被上傳"; 
        break; 
      case 3: 
       $erstr.="文件只被部分上傳"; 
        break; 
      case 2: 
       $erstr.="上傳文件超過了HTML表單MAX_FILE_SIZE指定的值"; 
        break; 
      case 1: 
       $erstr.="上傳文件超過了php.ini配置文件中upload_max_filesize的值"; 
        break; 
      case 0: 
       $erstr="上傳{$this->orginame}成功"; 
        break;        
      case -1: 
       $erstr="未允許的類型"; 
        break; 
      case -2: 
       $erstr.="文件過大,不能超過{$this->maxsize}個(gè)字節(jié)";  
        break; 
      case -3: 
       $erstr.="上傳失敗"; 
        break; 
      case -4: 
       $erstr="創(chuàng)建上傳目錄失敗,請(qǐng)重新指定上傳目錄"; 
        break; 
      case -5: 
       $erstr="未指定上傳路徑"; 
        break; 
      case -6: 
       $erstr="非法操作"; 
        break;                    
      default: 
       $erstr.="未知錯(cuò)誤"; 
         
    } 
    return $erstr; 
  } 
 } 
?> 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)php程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • PHP編程中的常見漏洞和代碼實(shí)例

    PHP編程中的常見漏洞和代碼實(shí)例

    這篇文章主要介紹了PHP編程中的常見漏洞和代碼實(shí)例,本文對(duì)編程中的一些常見漏洞做了總結(jié),并對(duì)如何防范做了講解,需要的朋友可以參考下
    2014-08-08
  • 淺談PHP的exec()函數(shù)無返回值排查方法(必看)

    淺談PHP的exec()函數(shù)無返回值排查方法(必看)

    下面小編就為大家?guī)硪黄獪\談PHP的exec()函數(shù)無返回值排查方法(必看)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-03-03
  • php 常用類匯總 推薦收藏

    php 常用類匯總 推薦收藏

    php 常用類總匯,充分利用,提高開發(fā)效率與增強(qiáng)php的功能實(shí)現(xiàn)。
    2010-05-05
  • 解析PHP中常見的mongodb查詢操作

    解析PHP中常見的mongodb查詢操作

    本篇文章是對(duì)PHP中常見的mongodb查詢操作進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06
  • PHP如何搭建百度Ueditor富文本編輯器

    PHP如何搭建百度Ueditor富文本編輯器

    這篇文章主要為大家詳細(xì)介紹了PHP搭建百度Ueditor富文本編輯器的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-09-09
  • php字符串截取的簡(jiǎn)單方法

    php字符串截取的簡(jiǎn)單方法

    這篇文章介紹了php字符串截取的簡(jiǎn)單方法,有需要的朋友可以參考一下
    2013-07-07
  • PHP的靜態(tài)方法與普通方法用法實(shí)例分析

    PHP的靜態(tài)方法與普通方法用法實(shí)例分析

    這篇文章主要介紹了PHP的靜態(tài)方法與普通方法用法,結(jié)合實(shí)例形式分析了PHP靜態(tài)方法與普通方法的功能、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下
    2019-09-09
  • php的RSA加密解密算法原理與用法分析

    php的RSA加密解密算法原理與用法分析

    這篇文章主要介紹了php的RSA加密解密算法原理與用法,結(jié)合實(shí)例形式分析了rsa加密解密算法的相關(guān)概念、原理及PHP使用RSA加密解密算法的具體實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2020-01-01
  • php正則preg_replace_callback函數(shù)用法實(shí)例

    php正則preg_replace_callback函數(shù)用法實(shí)例

    這篇文章主要介紹了php正則preg_replace_callback函數(shù)用法,實(shí)例分析了preg_replace_callback函數(shù)進(jìn)行正則替換的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06
  • PHP中l(wèi)ist方法用法示例

    PHP中l(wèi)ist方法用法示例

    這篇文章主要介紹了PHP中l(wèi)ist方法用法,結(jié)合實(shí)例形式分析了list方法實(shí)現(xiàn)將數(shù)組值賦給變量的相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下
    2016-12-12

最新評(píng)論

泗水县| 武夷山市| 大兴区| 左权县| 门源| 五大连池市| 东兰县| 芷江| 灵寿县| 许昌市| 灵山县| 长宁县| 井研县| 扎赉特旗| 大庆市| 鹤庆县| 湛江市| 怀远县| 民和| 平度市| 蒙城县| 岳池县| 佛教| 盈江县| 澳门| 泉州市| 阳朔县| 萨嘎县| 循化| 北票市| 安徽省| 桃江县| 莒南县| 白水县| 上杭县| 台东市| 阿克| 化德县| 房山区| 永善县| 沅江市|