標籤:檔案上傳類 php物件導向
<?php/***檔案上傳類***/class Upload{//上傳到哪個目錄protected $path = ‘./upload/‘;//准許的MIMEprotected $allowmime = [‘image/png‘,‘image/jpg‘,‘image/jpeg‘,‘image/pjpeg‘,‘image/bmp‘,‘image/wbmp‘,‘image/gif‘,‘image/x-png‘];//准許的尾碼protected $allowsubfix = [‘jpg‘,‘png‘,‘jpeg‘,‘gif‘,‘bmp‘];//准許的大小protected $allowsize = 2097152;//是否准許隨機檔案名稱protected $israndname = true;//是否准許日期目錄protected $isdatepath = true;//檔案的大小protected $size;//檔案的原名protected $orgname;//檔案的新名protected $newname;//檔案的尾碼protected $subfix;//檔案的MIME類型protected $mime;//檔案的新路徑protected $newpath;//錯誤號碼protected $errorno;//錯誤資訊protected $errorinfo;//臨時檔案protected $tmpfile;//首碼protected $prefix;//初使化成員屬性,傳進來的東西特別多,我們要求以數組的形式來傳遞//foreach迴圈,取出鍵名、值//將鍵名設定為成員屬性名,將索引值設為成員屬性值//注意鍵名的合法性// errorInfo errorinfopublic function __construct(Array $config = []){foreach ($config as $key => $value) {$this->setOption($key,$value);}}protected function setOption($key,$value){$key = strtolower($key);$allPro = get_class_vars(get_class($this));if (array_key_exists($key,$allPro)) {$this->$key = $value;}}//設定一個get方法專門獲得新路徑//設定 一個get方法專門來獲得錯誤資訊//成員方法上傳方法public function uploadFile($field){//檢測路徑使用者是否定義過,如果沒有定義失敗if (!file_exists($this->path)) {$this->setOption(‘errorNo‘,-1);return false;}//目錄許可權檢測if (!$this->checkPath()) {$this->setOption(‘errorNo‘,-2);return false;}//獲得$_FILES當中五個基本資料$name = $_FILES[$field][‘name‘];$size = $_FILES[$field][‘size‘];$type = $_FILES[$field][‘type‘];$tmpName = $_FILES[$field][‘tmp_name‘];$error = $_FILES[$field][‘error‘];//傳入到一個成員方法裡面進行設定if (!$this->setFiles($name,$size,$type,$tmpName,$error)) {return false;}//檢測MIME是否合法,檢測尾碼是否合法,檢測檔案大小是否超過了自訂的大小if (!$this->checkMime() || !$this->checkSubfix() || !$this->checkSize()) {return false;}//新名$this->newname = $this->getNewName();//新路徑處理$this->newpath = $this->getNewPath();//是否是上傳檔案//如果是上傳檔案移動上傳檔案至指定的目錄//如果成員return truereturn $this->move();}protected function move(){if (!is_uploaded_file($this->tmpfile)) {$this->setOption(‘errorNo‘,-6);return false;}if (move_uploaded_file($this->tmpfile,$this->newpath.$this->newname)) {return true;} else {$this->setOption(‘errorNo‘,-7);return false;}}protected function getNewPath(){$this->path = rtrim($this->path,‘/‘).‘/‘;if ($this->isdatepath) {$newpath = $this->path.date(‘Y/m/d/‘);if (!file_exists($newpath)) {mkdir($newpath,0755,true);}return $newpath;} else {return $this->path;}}protected function getNewName(){if ($this->israndname) {return $this->prefix . uniqid() . ‘.‘ . $this->subfix;} else {return $this->prefix . $this->orgname;}}protected function checkSize(){if ($this->size > $this->allowsize) {$this->setOption(‘errorNo‘,-5);return false;} else {return true;}}protected function checkSubFix(){if (in_array($this->subfix,$this->allowsubfix)) {return true;} else {$this->setOption(‘errorNo‘,-4);return false;}}protected function checkMime(){if (in_array($this->mime,$this->allowmime)) {return true;} else {$this->setOption(‘errorNo‘,-3);return false;}}protected function setFiles($name,$size,$type,$tmpName,$error) {//1 2 3 4 6 7 0(正常)if ($error) {$this->setOption(‘errorNo‘,$error);return false;}$this->orgname = $name;$this->size = $size;$this->tmpfile = $tmpName;$this->mime = $type;//尾碼沒處理$info = pathinfo($name);$this->subfix = $info[‘extension‘];return true;/*$arr = explode(‘.‘,$name);$this->subfix = array_pop($arr);$arr = explode(‘.‘,$name);$this->subfix = $arr[count($arr)-1];$pos = strrpos($name,‘.‘);echo substr($name,$pos + 1);*/}protected function checkPath(){//檢測路徑是否是目錄,如果不存在建立if (!is_dir($this->path)) {return mkdir($this->path,0755,true);}//檢測路徑是否可寫,如果不寫寫更改許可權if (!is_writeable($this->path) || !is_readable($this->path)) {return chmod($this->path,0755);}return true;}protected function getErrorInfo(){switch ($this->errorno) {case 1:$str = ‘上傳的檔案超過了 php.ini 中 upload_max_filesize 選項限制的值‘;break;case 2:$str = ‘上傳檔案的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值‘;break;case 3:$str = ‘部份件被上傳‘;break;case 4:$str = ‘沒有檔案被上傳‘;break;case 6:$str = ‘找不到臨時檔案夾‘;break;case 7:$str = ‘臨時檔案寫入失敗‘;break;case -1:$str = ‘自訂的上傳路徑不存在‘;break;case -2:$str = ‘沒有許可權‘;break;case -3:case -4:$str = ‘類型或尾碼不準許‘;break;case -5:$str = ‘超過了自訂的大小‘;break;case -6:$str = ‘不是上傳檔案‘;break;case -7:$str = ‘移動上傳檔案失敗‘;break;}return $str;}public function __get($key){if (in_array($key, [‘newpath‘,‘newname‘,‘errorno‘,‘size‘])) {return $this->$key;} else if ($key == ‘errorinfo‘) {return $this->getErrorInfo();}}}//調用:$upload = new Upload();$result = $upload->uploadFile(‘f‘);var_dump($result);
物件導向中的檔案上傳類