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

首頁 > 語言 > PHP > 正文

一個非常實用的php文件上傳類

2024-05-04 23:58:29
字體:
供稿:網(wǎng)友

其實網(wǎng)上已經(jīng)有很多這樣的類了,不過出于練手的目的還是自己仿照著寫了一個。

下面的代碼放在一個名為UploadFile.class.php文件內(nèi)

<?php  /**   * 文件上傳   * author:師少兵   * email :beibeijing163@163.com   * time :2012/12/09   */  class UploadFile{    private $max_size   = '2000000'; //設(shè)置上傳文件的大小,此為2M    private $rand_name   = true;   //是否采用隨機命名    private $allow_type  = array();  //允許上傳的文件擴展名    private $error     = 0;     //錯誤代號    private $msg      = '';    //信息    private $new_name   = '';    //上傳后的文件名    private $save_path   = '';    //文件保存路徑    private $uploaded   = '';    //路徑.文件名    private $file     = '';    //等待上傳的文件    private $file_type   = array();  //文件類型    private $file_ext   = '';    //上傳文件的擴展名    private $file_name   = '';    //文件原名稱    private $file_size   = 0;     //文件大小    private $file_tmp_name = '';    //文件臨時名稱        /**     * 構(gòu)造函數(shù),初始化     * @param string $rand_name 是否隨機命名     * @param string $save_path 文件保存路徑     * @param string $allow_type 允許上傳類型        $allow_type可為數(shù)組  array('jpg', 'jpeg', 'png', 'gif');        $allow_type可為字符串 'jpg|jpeg|png|gif';中間可用' ', ',', ';', '|'分割     */    public function __construct($rand_name=true, $save_path='./upload/', $allow_type=''){      $this->rand_name = $rand_name;      $this->save_path = $save_path;      $this->allow_type = $this->get_allow_type($allow_type);    }        /**     * 上傳文件     * 在上傳文件前要做的工作     * (1) 獲取文件所有信息     * (2) 判斷上傳文件是否合法     * (3) 設(shè)置文件存放路徑     * (4) 是否重命名     * (5) 上傳完成     * @param array $file 上傳文件     *     $file須包含$file['name'], $file['size'], $file['error'], $file['tmp_name']     */    public function upload_file($file){      //$this->file   = $file;      $this->file_name   = $file['name'];      $this->file_size   = $file['size'];      $this->error     = $file['error'];      $this->file_tmp_name = $file['tmp_name'];            $this->ext = $this->get_file_type($this->file_name);            switch($this->error){        case 0: $this->msg = ''; break;        case 1: $this->msg = '超出了php.ini中文件大小'; break;        case 2: $this->msg = '超出了MAX_FILE_SIZE的文件大小'; break;        case 3: $this->msg = '文件被部分上傳'; break;        case 4: $this->msg = '沒有文件上傳'; break;        case 5: $this->msg = '文件大小為0'; break;        default: $this->msg = '上傳失敗'; break;      }      if($this->error==0 && is_uploaded_file($this->file_tmp_name)){        //檢測文件類型        if(in_array($this->ext, $this->allow_type)==false){          $this->msg = '文件類型不正確';          return false;        }        //檢測文件大小        if($this->file_size > $this->max_size){          $this->msg = '文件過大';          return false;        }      }      $this->set_file_name();      $this->uploaded = $this->save_path.$this->new_name;      if(move_uploaded_file($this->file_tmp_name, $this->uploaded)){        $this->msg = '文件上傳成功';        return true;      }else{        $this->msg = '文件上傳失敗';        return false;      }    }        /**    * 設(shè)置上傳后的文件名    * 當前的毫秒數(shù)和原擴展名為新文件名    */    public function set_file_name(){      if($this->rand_name==true){        $a = explode(' ', microtime());        $t = $a[1].($a[0]*1000000);        $this->new_name = $t.'.'.($this->ext);      }else{        $this->new_name = $this->file_name;      }    }        /**    * 獲取上傳文件類型    * @param string $filename 目標文件    * @return string $ext 文件類型    */    public function get_file_type($filename){      $ext = pathinfo($filename, PATHINFO_EXTENSION);      return $ext;    }        /**     * 獲取可上傳文件的類型     */    public function get_allow_type($allow_type){      $s = array();      if(is_array($allow_type)){        foreach($allow_type as $value){          $s[] = $value;        }      }else{        $s = preg_split("/[/s,|;]+/", $allow_type);      }      return $s;    }        //獲取錯誤信息    public function get_msg(){      return $this->msg;    }  }?>

其實上面的代碼中還有一個可以改進的地方,就是將那些以‘file_'開頭的變量縮寫為一個$file數(shù)組,這樣感覺更好一些。

下面我們來測試一下上面的代碼。我在一個名為upfile.php文件寫測試代碼,同時將UploadFile.class.php放在同一個路徑下。

<html><head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <title>upfile</title></head><body>  <?php    require 'UploadFile.class.php';    if(isset($_POST['sf']) && $_POST['sf']=='sf'){      if ($_FILES["file"]["error"] > 0){        echo "Error: " . $_FILES["file"]["error"] . "<br />";      }else{        $file = $_FILES['file'];                $upload = new UploadFile(true, './images/', array('jpg', 'jpeg', 'png'));        $upload->upload_file($file);        echo $upload->get_msg();      }    }else{  ?>  <form action="" method='post' enctype="multipart/form-data">    <input type="file" name="file" id="file" />    <input type="hidden" name="sf" value="sf"/>    <input type="submit" value="上傳" name="sub" />  </form>  <?php    }  ?></body></html>

在上面的代碼中,我們可以嘗試修改第15行的參數(shù),用來判斷一下我們寫的方法是否正確。

這3個參數(shù)的含義分別表示:是否使用系統(tǒng)命名、文件存放的路徑(相對)、允許上傳的文件類型。那么就試試修改這3個參數(shù)會發(fā)生什么樣的變化:(1)把true改為false是否就可以使用它原來的名字了;(2)改下存放路徑,看看能不能依然能夠上傳;(3)試試上傳幾個不允許的文件,看能不能禁止住,而且關(guān)鍵第三個參數(shù)有兩種形式,一種是數(shù)組,就像示例中一樣;還有一種是字符串,用分隔符隔開就行, 'jpg|jpeg|png|gif', 'jpg jpeg png gif', 'jpg,jpeg,png,gif'都行。

好的,文件上傳類就這樣寫好了。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 保定市| 永济市| 马鞍山市| 成武县| 沾化县| 上犹县| 承德市| 鹰潭市| 黑龙江省| 柯坪县| 康保县| 阿鲁科尔沁旗| 兴海县| 克什克腾旗| 桃源县| 海口市| 仁布县| 阳信县| 商都县| 富锦市| 南昌县| 南乐县| 饶河县| 韩城市| 新营市| 长葛市| 广南县| 三台县| 屏东市| 瑞金市| 金湖县| 石首市| 偏关县| 鄂州市| 濮阳市| 英吉沙县| 柘城县| 平陆县| 凯里市| 南昌市| 丹巴县|