====================ImageUploadTool========================

<?php class ImageUploadTool { private $file; //文件信息 private $fileList; //文件列表 private $inputName; //標簽名稱 private $uploadPath; //上傳路徑 private $fileMaxSize; //最大尺寸 private $uploadFiles; //上傳文件 //允許上傳的文件類型 private $allowExt = array('bmp', 'jpg', 'jpeg', 'png', 'gif'); /** * ImageUploadTool constructor. * @param $inputName input標簽的name屬性 * @param $uploadPath 文件上傳路徑 */ public function __construct($inputName, $uploadPath) { $this->inputName = $inputName; $this->uploadPath = $uploadPath; $this->fileList = array(); $this->file = $file = array( 'name' => null, 'type' => null, 'tmp_name' => null, 'size' => null, 'errno' => null, 'error' => null ); } /** * 設置允許上傳的圖片后綴格式 * @param $allowExt 文件后綴數組 */ public function setAllowExt($allowExt) { if (is_array($allowExt)) { $this->allowExt = $allowExt; } else { $this->allowExt = array($allowExt); } } /** * 設置允許上傳的圖片規格 * @param $fileMaxSize 最大文件尺寸 */ public function setMaxSize($fileMaxSize) { $this->fileMaxSize = $fileMaxSize; } /** * 獲取上傳成功的文件數組 * @return mixed */ public function getUploadFiles() { return $this->uploadFiles; } /** * 得到文件上傳的錯誤信息 * @return array|mixed */ public function getErrorMsg() { if (count($this->fileList) == 0) { return $this->file['error']; } else { $errArr = array(); foreach ($this->fileList as $item) { array_push($errArr, $item['error']); } return $errArr; } } /** * 初始化文件參數 * @param $isList */ private function initFile($isList) { if ($isList) { foreach ($_FILES[$this->inputName] as $key => $item) { for ($i = 0; $i < count($item); $i++) { if ($key == 'error') { $this->fileList[$i]['error'] = null; $this->fileList[$i]['errno'] = $item[$i]; } else { $this->fileList[$i][$key] = $item[$i]; } } } } else { $this->file['name'] = $_FILES[$this->inputName]['name']; $this->file['type'] = $_FILES[$this->inputName]['type']; $this->file['tmp_name'] = $_FILES[$this->inputName]['tmp_name']; $this->file['size'] = $_FILES[$this->inputName]['size']; $this->file['errno'] = $_FILES[$this->inputName]['error']; } } /** * 上傳錯誤檢查 * @param $errno * @return null|string */ private function errorCheck($errno) { switch ($errno) { case UPLOAD_ERR_OK: return null; case UPLOAD_ERR_INI_SIZE: return '文件尺寸超過服務器限制'; case UPLOAD_ERR_FORM_SIZE: return '文件尺寸超過表單限制'; case UPLOAD_ERR_PARTIAL: return '只有部分文件被上傳'; case UPLOAD_ERR_NO_FILE: return '沒有文件被上傳'; case UPLOAD_ERR_NO_TMP_DIR: return '找不到臨時文件夾'; case UPLOAD_ERR_CANT_WRITE: return '文件寫入失敗'; case UPLOAD_ERR_EXTENSION: return '上傳被擴展程序中斷'; } } /** * 上傳文件校驗 * @param $file * @throws Exception */ private function fileCheck($file) { //圖片上傳過程是否順利 if ($file['errno'] != 0) { $error = $this->errorCheck($file['errno']); throw new Exception($error); } //圖片尺寸是否符合要求 if (!empty($this->fileMaxSize) && $file['size'] > $this->fileMaxSize) { throw new Exception('文件尺寸超過' . ($this->fileMaxSize / 1024) . 'KB'); } //圖片類型是否符合要求 $ext = pathinfo($file['name'], PATHINFO_EXTENSION); if (!in_array($ext, $this->allowExt)) { throw new Exception('不符合要求的文件類型'); } //圖片上傳方式是否為HTTP if (!is_uploaded_file($file['tmp_name'])) { throw new Exception('文件不是通過HTTP方式上傳的'); } //圖片是否可以讀取 if (!getimagesize($file['tmp_name'])) { throw new Exception('圖片文件損壞'); } //檢查上傳路徑是否存在 if (!file_exists($this->uploadPath)) { mkdir($this->uploadPath, null, true); } } /** * 單文件上傳,成功返回true * @return bool */ public function acceptSingleFile() { $this->initFile(false); try { $this->fileCheck($this->file); $md_name = md5(uniqid(microtime(true), true)) . '.' . pathinfo($this->file['name'], PATHINFO_EXTENSION); if (move_uploaded_file($this->file['tmp_name'], $this->uploadPath . $md_name)) { $this->uploadFiles = array($this->uploadPath . $md_name); } else { throw new Exception('文件上傳失敗'); } } catch (Exception $e) { $this->file['error'] = $e->getMessage(); } finally { if (file_exists($this->file['tmp_name'])) { unlink($this->file['tmp_name']); } } return empty($this->file['error']) ? true : false; } /** * 多文件上傳,全部成功返回true * @return bool */ public function acceptMultiFile() { $this->initFile(true); $this->uploadFiles = array(); for ($i = 0; $i < count($this->fileList); $i++) { try { $this->fileCheck($this->fileList[$i]); $ext = pathinfo($this->fileList[$i]['name'], PATHINFO_EXTENSION); $md_name = md5(uniqid(microtime(true), true)) . '.' . $ext; if (move_uploaded_file($this->fileList[$i]['tmp_name'], $this->uploadPath . $md_name)) { array_push($this->uploadFiles, $this->uploadPath . $md_name); } else { throw new Exception('文件上傳失敗'); } } catch (Exception $e) { $this->fileList[$i]['error'] = $e->getMessage(); } finally { if (file_exists($this->fileList[$i]['tmp_name'])) { unlink($this->fileList[$i]['tmp_name']); } } } foreach ($this->fileList as $item) { if (!empty($item['error'])) { return false; } } return true; } }
=======================單文件上傳===========================
首先創建一個簡單HTML表單:
<form action="tool_test.php" method="post" enctype="multipart/form-data"> <!--通過表單隱藏域限制上傳文件的最大值--> <!--<input type="hidden" name="MAX_FILE_SIZE" value="204800">--> <!--通過accept屬性限制上傳文件類型--> <!--<input type="hidden" name="myfile" accept="image/jpeg,image/png">--> <label for="upload">請選擇上傳的文件</label> <input type="file" id="upload" name="myfile"><br> <input type="submit" value="上傳文件"> </form>
再創建一個tool_test.php文件:
<?php //引用圖片上傳工具 require_once 'ImageUploadTool.class.php'; //初始化上傳工具,參數(input表單name屬性 , 文件上傳路徑) $uploadTool = new ImageUploadTool('myfile', 'file/'); //進行單文件上傳操作,上傳成功返回true if ($uploadTool->acceptSingleFile()) { //獲取上傳后的文件路徑,並用img標簽顯示 echo "<img src='{$uploadTool->getUploadFiles()[0]}'/>"; } else { //打印錯誤信息 echo $uploadTool->getErrorMsg(); }
=======================多文件上傳===========================
首先創建一個簡單HTML表單:
需要在<input type="file">標簽的name屬性后面追加“[]”,並且添加 multiple 屬性
<form action="tool_test.php" method="post" enctype="multipart/form-data"> <label for="upload">請選擇上傳的文件</label> <input type="file" id="upload" name="myfile[]" multiple><br> <input type="submit" value="上傳文件"> </form>
再創建一個tool_test.php文件:
<?php //引用圖片上傳工具 require_once 'ImageUploadTool.class.php'; //初始化上傳工具,參數(input表單name屬性 , 文件上傳路徑) $uploadTool = new ImageUploadTool('myfile', 'file/'); //設置允許上傳的文件后綴類型 $uploadTool->setAllowExt(array('png', 'jpg')); //設置上傳文件的最大尺寸 $uploadTool->setMaxSize(1024 * 200); //進行多文件上傳操作,全部上傳成功返回true $uploadTool->acceptMultiFile(); //打印所有上傳成功的圖片路徑 print_r($uploadTool->getUploadFiles()); //打印所有上傳失敗的錯誤信息 print_r($uploadTool->getErrorMsg());
多文件上傳測試: