其實網(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; //是否采用隨機(jī)命名 private $allow_type = array(); //允許上傳的文件擴(kuò)展名 private $error = 0; //錯誤代號 private $msg = ''; //信息 private $new_name = ''; //上傳后的文件名 private $save_path = ''; //文件保存路徑 private $uploaded = ''; //路徑.文件名 private $file = ''; //等待上傳的文件 private $file_type = array(); //文件類型 private $file_ext = ''; //上傳文件的擴(kuò)展名 private $file_name = ''; //文件原名稱 private $file_size = 0; //文件大小 private $file_tmp_name = ''; //文件臨時名稱 /** * 構(gòu)造函數(shù),初始化 * @param string $rand_name 是否隨機(jī)命名 * @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è)置上傳后的文件名 * 當(dāng)前的毫秒數(shù)和原擴(kuò)展名為新文件名 */ 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 目標(biāo)文件 * @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; } } ?>
其實上面的代碼中還有一個可以改進(jìn)的地方,就是將那些以‘file_'開頭的變量縮寫為一個$file數(shù)組,這樣感覺更好一些。
【網(wǎng)站聲明】本站除付費源碼經(jīng)過測試外,其他素材未做測試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請勿用于商業(yè)用途。如損害你的權(quán)益請聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。