<?php $up = new Upload(); $newPath = $up->uploadFile('fm'); if ($newPath === false) { var_dump($up->errorNumber); var_dump($up->errorInfo); } else { echo $newPath; } class Upload { //文件上傳保存路徑 protected $path = './upload/'; //容許文件上傳的後綴 protected $allowSuffix = ['jpg', 'jpeg', 'gif', 'wbmp', 'png']; //容許文件上傳的Mime類型 protected $allowMime = ['image/jpeg', 'image/gif', 'image/wbmp', 'image/png']; //容許文件上傳的文件最大大小 protected $maxSize = 2000000; //是否啓用隨機名 protected $isRandName = true; //加上文件前綴 protected $prefix = 'up_'; //自定義的錯誤號碼和錯誤信息 protected $errorNumber; protected $errorInfo; //文件的信息 protected $oldName; //文件名 protected $suffix; //文件後綴 protected $size; //文件大小 protected $mime; //文件mime protected $tmpName; //文件臨時路徑 protected $newName; //文件新名字 public function __construct($arr = []) { foreach ($arr as $key => $value) { $this->setOption($key, $value); } } public function __get($name) { if ($name == 'errorNumber') { return $this->errorNumber; } else if ($name == 'errorInfo') { return $this->getErrorInfo(); } } /** * 判斷這個$key是否是個人成員屬性,若是是,則設置 * * @param [type] $key * @param [type] $value * @return void */ protected function setOption($key, $value) { //獲得全部的成員屬性 $keys = array_keys(get_class_vars(__CLASS__)); if (in_array($key, $keys)) { $this->$key = $value; } } /** * 文件上傳函數 * $key 就是你input框中的name屬性值 * * @param [type] $key * @return void */ public function uploadFile($key) { //判斷有沒有設置路徑 path if (empty($this->path)) { $this->setOption('errorNumber', -1); return false; } //判斷該路徑是否存在,是否可寫 if (!$this->check()) { $this->setOption('errorNumber', -2); return false; } //判斷$_FILES裏面的error信息是否爲0,若是爲0,說明文件信息在服務器端能夠直接獲取,提取信息保存到成員屬性中 $error = $_FILES[$key]['error']; if ($error) { $this->setOption('errorNumber', $error); return false; } else { //提取文件相關信息而且保存到成員屬性中 $this->getFileInfo($key); } //判斷文件的大小、mime、後綴是否符合 if (!$this->checkSize() || !$this->checkMime() || !$this->checkSuffix()) { return false; } //獲得新的文件名字 $this->newName = $this->createNewName(); //判斷是不是上傳文件,而且移動上傳文件 if (is_uploaded_file($this->tmpName)) { if (move_uploaded_file($this->tmpName, $this->path . $this->newName)) { return $this->path . $this->newName; } else { $this->setOption('errorNumber', -7); return false; } } else { $this->setOption('errorNumber', -6); return false; } } /** * 獲得文件的新名字 * * @return void */ protected function createNewName() { //判斷是否使用隨機名 if ($this->isRandName) { $name = $this->prefix . uniqid() . '.' . $this->suffix; } else { $name = $this->prefix . $this->oldName; } return $name; } /** * 判斷該路徑是否存在,是否可寫 * * @return void */ protected function check() { //文件夾不存在或者不是目錄。建立文件夾 if (!file_exists($this->path) || !is_dir($this->path)) { return mkdir($this->path, 0777, true); } //判斷文件是否可寫 if (!is_writeable($this->path)) { return chmod($this->path, 0777); } return true; } /** * 提取文件相關信息而且保存到成員屬性中 * * @param [type] $key * @return void */ protected function getFileInfo($key) { // 獲得文件名字 $this->oldName = $_FILES[$key]['name']; //獲得文件的mime類型 $this->mime = $_FILES[$key]['type']; //獲得文件臨時路徑 $this->tmpName = $_FILES[$key]['tmp_name']; //獲得文件大小 $this->size = $_FILES[$key]['size']; //獲得文件後綴 $this->suffix = pathinfo($this->oldName)['extension']; } /** * 判斷文件大小 * * @return void */ protected function checkSize() { if ($this->size > $this->maxSize) { $this->setOption('errorNumber', -3); return false; } return true; } /** * 判斷文件的mime是否符合 * * @return void */ protected function checkMime() { if (!in_array($this->mime, $this->allowMime)) { $this->setOption('errorNumber', -4); return false; } return true; } /** * 判斷文件的後綴是否符合 * * @return void */ protected function checkSuffix() { if (!in_array($this->suffix, $this->allowSuffix)) { $this->setOption('errorNumber', -5); return false; } return true; } /** * 返回錯誤信息 * * @return void */ protected function getErrorInfo() { switch ($this->errorNumber) { case -1: $str = '文件路徑沒有設置'; break; case -2: $str = '文件路徑不是目錄或者沒有權限'; break; case -3: $str = '文件大小超過指定範圍'; break; case -4: $str = '文件mime類型不符合'; break; case -5: $str = '文件後綴不符合'; break; case -6: $str = '不是上傳文件'; break; case -7: $str = '文件上傳失敗'; break; case 1: $str = '文件超出php.ini設置大小'; break; case 2: $str = '文件超出html設置大小'; break; case 3: $str = '文件部分上傳'; break; case 4: $str = '沒有文件上傳'; break; case 6: $str = '找不到臨時文件'; break; case 7: $str = '文件寫入失敗'; break; } return $str; } }
測試代碼:php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>上傳文件</title> </head> <body> <form action="Upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="fm"> <br /> <input type="submit" value="上傳"> </form> </body> </html>
運行結果:html