非常經典的PHP檔案上傳類分享,_PHP教程

來源:互聯網
上載者:User

非常經典的PHP檔案上傳類分享,


檔案上傳是項目開發中比較常見的功能,但檔案上傳的過程比較繁瑣,只要是有檔案上傳的地方就需要編寫這些複雜的代碼。為了能在每次開發中降低功能的編寫難度,也為了能節省開發時間,通常我們都會將這些反覆使用的一段代碼封裝到一個類中。

<?php /**  +-----------------------------------------------------------------------------  * 檔案上傳類  +-----------------------------------------------------------------------------  * @author Administrator  +-----------------------------------------------------------------------------  */  class FileUpload{      private $filepath;  //儲存路徑   private $allowtype=array('gif','jpg','jpeg','png','txt');   private $maxsize=1000000;  //最大允許上傳大小   private $israndname=true; //是否隨機   private $orginame; //原始檔案名   private $tmpname;  //臨時檔案名稱   private $newname;  //新檔案名稱   private $filetype; //檔案類型    private $filesize; //檔案大小   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'];     $tmpname=$_FILES[$field]['tmp_name'];     $filesize=$_FILES[$field]['size'];     $error=$_FILES[$field]['error'];       if (is_array($name)) {       $errors=array();        for ($i=0;$igetFile($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;$igetFile($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;     }   }   /**    +------------------------------------------------------------------------    *設定類屬性值函數    +------------------------------------------------------------------------    * @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($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;   }   /**    +-------------------------------------------------------------------------    * 檢查上傳路徑函數    +-------------------------------------------------------------------------    * @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="上傳檔案{$this->orginame}出錯";     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;   }  } ?> 

以上就是本文的全部內容,希望對大家學習php程式設計有所協助。

http://www.bkjia.com/PHPjc/1127897.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1127897.htmlTechArticle非常經典的PHP檔案上傳類分享, 檔案上傳 是項目開發中比較常見的功能,但檔案上傳的過程比較繁瑣,只要是有檔案上傳的地方就需要編...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.