php文件上傳類

 <?php
/**
 +----------------------------------------------------------------------------- 
 * 文件上傳類
 +-----------------------------------------------------------------------------
 * @author Administrator
 +-----------------------------------------------------------------------------
 */
  class FileUpload{
      
      private $filepath;   //保存路徑
      private $allowtype=array('gif','jpg','jpeg','png');
      private $maxsize=1000000;   //最大容許上傳大小
      private $israndname=true;  //是否隨機
      private $saverule='';//命名規則
      private $orginame;  //原始文件名
      private $tmp_name;   //臨時文件名
      private $newname;   //新文件名
      private $filetype;  //文件類型 
      private $size;  //文件大小
      private $errornum=''; //錯誤號
      private $errormsg;  //錯誤信息
/**
 +------------------------------------------------------------------------------ 
 *構造函數
 +------------------------------------------------------------------------------ 
 * @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'];
          var_dump($_FILES[$field]);exit();
          $tmpname=$_FILES[$field]['tmp_name'];
          $size=$_FILES[$field]['size'];
          $error=$_FILES[$field]['error'];*/
            $filearr=$_FILES[$field]; 
             if($this->getFile($filearr)){
                     if($this->CheckSize()&&$this->CheckType()){
                            $this->SetFileName();
                         if ($this->MoveFile()) {
                             return true;
                         }else{
                             return false;
                         }                                                                                                   
                  }else{
                       return  false;
                  }  
             }else{
                  return false;
             }
             if (!$return) {
                 $this->setOption('ErrorNum', 0);
                 $this->errormsg=$this->geterrorNum();
             }
            return $return; 
      }
      /**
       +------------------------------------------------------------------------
       *設置類屬性值函數
       +------------------------------------------------------------------------
       * @param mix $key
       * @param mix $value
       */
      private function setOption($key,$value){
          $key=strtolower($key);
          $this->$key=$value;
      }
      /**
       +--------------------------------------------------------------------------- 
       * 獲取文件變量參數函數
       +---------------------------------------------------------------------------
       * @param string $name
       * @param string $tmp_name
       * @param number $size
       * @param number $error
       */
      private function getFile($option=array()){
          foreach($option as $key=>$value){
              if ($key=='size'||$key=='tmp_name') {
              $this->setOption($key, $value);
              }
          }         
          //$this->setOption('TmpName', $tmp_name);
          $this->setOption('OrgiName', $option['name']);
          $arrstr=explode('.', $option['name']);
          $this->setOption('FileType', $arrstr[count($arrstr)-1]);          
          //$this->setOption('FileSize', $size);*/
          return true;
      }
      /**
       +-------------------------------------------------------------------------
       * 檢查上傳路徑函數
       +-------------------------------------------------------------------------
       * @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;
          }
      }
      /**
       +-------------------------------------------------------------------- 
       *檢查文件尺寸函數
       +--------------------------------------------------------------------
       * @return boolean
       */
      private function CheckSize(){
          if ($this->filesize>$this->maxsize) {
              $this->setOption('ErrorNum', -2);
              return false;
          }else{
              return true;
          }
      }
      /**
       +--------------------------------------------------------------- 
       * 檢查文件類型函數
       +--------------------------------------------------------------- 
       * @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;
          }
      }
      /**
       +---------------------------------------------------------------- 
       * 錯誤信息函數
       +----------------------------------------------------------------
       * @return string
       */
       function getErrorNum() {
          $erstr="上傳文件<font color='red'>{$this->orginame}</font>出錯";
          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}個字節"; 
                  break;
              case -3:
               $erstr.="上傳失敗";
                  break;
              case -4:
               $erstr="建立上傳目錄失敗,請從新指定上傳目錄";
                  break;
              case -5:
               $erstr="未指定上傳路徑";
                  break;
              case -6:
               $erstr="非法操做";
                  break;                                           
              default:
               $erstr.="未知錯誤";
                  
          }
          return  $erstr;
      }
  }
?>
相關文章
相關標籤/搜索