茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    非常經(jīng)典的PHP文件上傳類分享
    來源:易賢網(wǎng) 閱讀:1186 次 日期:2016-08-22 15:21:44
    溫馨提示:易賢網(wǎng)小編為您整理了“非常經(jīng)典的PHP文件上傳類分享”,方便廣大網(wǎng)友查閱!

    文件上傳是項(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ì)有所幫助。

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    易賢網(wǎng)手機(jī)網(wǎng)站地址:非常經(jīng)典的PHP文件上傳類分享
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報(bào)班

    • 報(bào)班類型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專用圖標(biāo)