您好,登錄后才能下訂單哦!
<?php /** *文件上傳類 * **/ class Upload { //上傳到哪個目錄 protected $path = './upload/'; //準許的MIME protected $allowmime = ['p_w_picpath/png','p_w_picpath/jpg','p_w_picpath/jpeg','p_w_picpath/pjpeg','p_w_picpath/bmp','p_w_picpath/wbmp','p_w_picpath/gif','p_w_picpath/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 errorinfo public 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 true return $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);
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。