單文件上傳前端頁面 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Upload A File</title> </head> <body> <form action="upload.php" method= ...
單文件上傳
前端頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Upload A File</title> </head> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="1024000" /> <input type="file" name="test_pic" /> <input type="submit" value="上傳" /> </form> </body> </html>
後端實現upload.php
<?php /** * Created by PhpStorm. * Date: 2018/11/11 * Time: 14:06 */ // 接收$_FILES數組 $key = 'test_pic'; $mimeWhiteList = ['image/jpeg', 'image/png', 'image/gif'];//文件映射白名單 $extWhiteList = ['jpeg', 'jpg', 'png', 'gif'];//文件擴展名白名單 $allowSize = 2*1024*1024; $destDir = './uploads'; $name = $_FILES[$key]['name']; // 源文件名稱 $type = $_FILES[$key]['type']; // MIME 類型 $tmpName = $_FILES[$key]['tmp_name']; // 臨時文件名稱 $error = $_FILES[$key]['error']; // 錯誤信息 $size = $_FILES[$key]['size']; // 文件大小 位元組 // 處理錯誤 // 0 - 無錯誤 if ($error > UPLOAD_ERR_OK) { switch($error) { // 1 - 文件大小超出了php.ini當中的upload_max_filesize的大小 case UPLOAD_ERR_INI_SIZE: exit('文件大小超出了php.ini當中的upload_max_filesize的大小'); // 2 - 超出表單當中的MAX_FILE_SIZE的大小 case UPLOAD_ERR_FORM_SIZE: exit('超出表單當中的MAX_FILE_SIZE的大小'); // 3 - 部分文件被上傳 case UPLOAD_ERR_PARTIAL: exit('部分文件被上傳'); // 4 - 沒有文件被上傳 case UPLOAD_ERR_NO_FILE: exit('沒有文件被上傳'); // 6 - 臨時目錄不存在 case UPLOAD_ERR_NO_TMP_DIR: exit('臨時目錄不存在'); // 7 - 磁碟寫入失敗 case UPLOAD_ERR_CANT_WRITE: exit('磁碟寫入失敗'); // 8 - 文件上傳被PHP擴展阻止 case UPLOAD_ERR_EXTENSION: exit('文件上傳被PHP擴展阻止'); default: exit('未知錯誤'); } } // 限制文件的MIME if (!in_array($type, $mimeWhiteList)) { exit('文件類型' . $type . '不被允許!'); } // 限制文件的擴展名 $ext = pathinfo($name, PATHINFO_EXTENSION); if (!in_array($ext, $extWhiteList)) { exit('文件擴展名' . $ext . '不被允許!'); } // 限制文件大小 if ($size > $allowSize) { exit('文件大小 ' . $size . ' 超出限定大小 ' . $allowSize . ' !'); } // 生成新的隨機文件名稱 // md5(rand()); $fileName = uniqid() . '.' . $ext; // 移動臨時文件到指定目錄當中並重新命名文件名 if (!file_exists($destDir)) { mkdir($destDir, 0777, true); } if (is_uploaded_file($tmpName) && move_uploaded_file($tmpName, $destDir . '/' . $fileName)) { echo "恭喜,文件上傳成功"; } else { echo "很抱歉,文件上傳失敗"; }
多文件上傳,指定文件數量
前端頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Upload Multiple Files</title> </head> <body> <form action="multiple_upload.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="1024000" /> <input type="file" name="test_pic1" /> <input type="file" name="test_pic2" /> <input type="file" name="test_pic3" /> <input type="submit" value="上傳" /> </form> </body> </html>
後端實現multiple_upload.php
<?php /** * Created by PhpStorm. * Date: 2018/11/11 * Time: 14:54 */ $errors = []; $mimeWhiteList = ['image/jpeg', 'image/png', 'image/gif']; $extWhiteList = ['jpeg', 'jpg', 'png', 'gif']; $allowSize = 2*1024*1024; $destDir = './uploads'; foreach($_FILES as $key => $val) { // name type tmp_name error size // 接收$_FILES $name = $_FILES[$key]['name']; // 源文件名稱 $type = $_FILES[$key]['type']; // MIME 類型 $tmpName = $_FILES[$key]['tmp_name']; // 臨時文件名稱 $error = $_FILES[$key]['error']; // 錯誤信息 $size = $_FILES[$key]['size']; // 文件大小 位元組 // 處理錯誤 // 0 - 無錯誤 if ($error > UPLOAD_ERR_OK) { switch($error) { // 1 - 文件大小超出了php.ini當中的upload_max_filesize的大小 case UPLOAD_ERR_INI_SIZE: $errors[$key] = '文件大小超出了php.ini當中的upload_max_filesize的大小'; continue 2; // 2 - 超出表單當中的MAX_FILE_SIZE的大小 case UPLOAD_ERR_FORM_SIZE: $errors[$key] = '超出表單當中的MAX_FILE_SIZE的大小'; continue 2; // 3 - 部分文件被上傳 case UPLOAD_ERR_PARTIAL: $errors[$key] = '部分文件被上傳'; continue 2; // 4 - 沒有文件被上傳 case UPLOAD_ERR_NO_FILE: $errors[$key] = '沒有文件被上傳'; continue 2; // 6 - 臨時目錄不存在 case UPLOAD_ERR_NO_TMP_DIR: $errors[$key] = '臨時目錄不存在'; continue 2; // 7 - 磁碟寫入失敗 case UPLOAD_ERR_CANT_WRITE: $errors[$key] = '磁碟寫入失敗'; continue 2; // 8 - 文件上傳被PHP擴展阻止 case UPLOAD_ERR_EXTENSION: $errors[$key] = '文件上傳被PHP擴展阻止'; continue 2; default: $errors[$key] = '未知錯誤'; continue 2; } } // 限制MIME if (!in_array($type, $mimeWhiteList)) { $errors[$key] = '文件類型' . $type . '不被允許!'; continue; } // 限制擴展名 $ext = pathinfo($name, PATHINFO_EXTENSION); if (!in_array($ext, $extWhiteList)) { $errors[$key] = '文件擴展名' . $ext . '不被允許!'; continue; } // 限制大小 if ($size > $allowSize) { $errors[$key] = '文件大小 ' . $size . ' 超出限定大小 ' . $allowSize . ' !'; continue; } // 生成隨機文件名稱 $fileName = uniqid() . '.' . $ext; // 移動文件 if (!file_exists($destDir)) { mkdir($destDir, 0777, true); } if (!is_uploaded_file($tmpName) || !move_uploaded_file($tmpName, $destDir . '/' . $fileName)) { $errors[$key] = "很抱歉,文件上傳失敗"; continue; } } if (count($errors) > 0) { var_dump($errors); } else { echo "文件全部上傳成功"; }
多文件上傳,不定文件數量
前端頁面
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Upload Multiple Files</title> </head> <body> <form action="multiple_upload2.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="6024000" /> <input type="file" name="test_pic[]" /> <input type="file" name="test_pic[]" /> <input type="file" name="test_pic[]" /> <input type="submit" value="上傳" /> </form> </body> </html>
後端實現multiple_upload2.php
<?php /** * Created by PhpStorm. * Date: 2018/11/11 * Time: 15:12 */ $key = 'test_pic'; $mimeWhiteList = ['image/jpeg', 'image/png', 'image/gif']; $extWhiteList = ['jpeg', 'jpg', 'png', 'gif']; $allowSize = 2*1024*1024; $destDir = './uploads'; // 接收和處理$_FILES if (!empty($_FILES[$key])) { $files = []; foreach($_FILES[$key]['name'] as $k => $v) { $files[$k]['name'] = $v; $files[$k]['type'] = $_FILES[$key]['type'][$k]; $files[$k]['tmp_name'] = $_FILES[$key]['tmp_name'][$k]; $files[$k]['error'] = $_FILES[$key]['error'][$k]; $files[$k]['size'] = $_FILES[$key]['size'][$k]; } } $errors = []; foreach($files as $file) { // name type error size $name = $file['name']; // 源文件名稱 $type = $file['type']; // MIME 類型 $tmpName = $file['tmp_name']; // 臨時文件名稱 $error = $file['error']; // 錯誤信息 $size = $file['size']; // 文件大小 位元組 // 處理錯誤 // 0 - 無錯誤 if ($error > UPLOAD_ERR_OK) { switch($error) { // 1 - 文件大小超出了php.ini當中的upload_max_filesize的大小 case UPLOAD_ERR_INI_SIZE: $errors[$key] = $name . '文件大小超出了php.ini當中的upload_max_filesize的大小'; continue 2; // 2 - 超出表單當中的MAX_FILE_SIZE的大小 case UPLOAD_ERR_FORM_SIZE: $errors[$key] = $name . '超出表單當中的MAX_FILE_SIZE的大小'; continue 2; // 3 - 部分文件被上傳 case UPLOAD_ERR_PARTIAL: $errors[$key] = $name . '部分文件被上傳'; continue 2; // 4 - 沒有文件被上傳 case UPLOAD_ERR_NO_FILE: $errors[$key] = $name . '沒有文件被上傳'; continue 2; // 6 - 臨時目錄不存在 case UPLOAD_ERR_NO_TMP_DIR: $errors[$key] = $name . '臨時目錄不存在'; continue 2; // 7 - 磁碟寫入失敗 case UPLOAD_ERR_CANT_WRITE: $errors[$key] = $name . '磁碟寫入失敗'; continue 2; // 8 - 文件上傳被PHP擴展阻止 case UPLOAD_ERR_EXTENSION: $errors[$key] = $name . '文件上傳被PHP擴展阻止'; continue 2; default: $errors[$key] = $name . '未知錯誤'; continue 2; } } // 限制MIME類型 if (!in_array($type, $mimeWhiteList)) { $errors[$key] = '文件類型' . $type . '不被允許!'; continue; } // 限制擴展名 $ext = pathinfo($name, PATHINFO_EXTENSION); if (!in_array($ext, $extWhiteList)) { $errors[$key] = '文件擴展名' . $ext . '不被允許!'; continue; } // 限制文件大小 if ($size > $allowSize) { $errors[$key] = '文件大小 ' . $size . ' 超出限定大小 ' . $allowSize . ' !'; continue; } // 生成隨機文件名稱 $fileName = uniqid() . '.' . $ext; // 移動文件 if (!file_exists($destDir)) { mkdir($destDir, 0777, true); } if (!is_uploaded_file($tmpName) || !move_uploaded_file($tmpName, $destDir . '/' . $fileName)) { $errors[$key] = "很抱歉,文件上傳失敗"; continue; } } if (count($errors) > 0) { var_dump($errors); } else { echo "文件全部上傳成功"; }
文件上傳類封裝 UploadFile.php
支持多文件或者單文件
<?php /** * Created by PhpStorm. * Date: 2018/11/11 * Time: 22:01 */ class UploadFile { /** * */ const UPLOAD_ERROR = [ UPLOAD_ERR_INI_SIZE => '文件大小超出了php.ini當中的upload_max_filesize的值', UPLOAD_ERR_FORM_SIZE => '文件大小超出了MAX_FILE_SIZE的值', UPLOAD_ERR_PARTIAL => '文件只有部分被上傳', UPLOAD_ERR_NO_FILE => '沒有文件被上傳', UPLOAD_ERR_NO_TMP_DIR => '找不到臨時目錄', UPLOAD_ERR_CANT_WRITE => '寫入磁碟失敗', UPLOAD_ERR_EXTENSION => '文件上傳被擴展阻止', ]; /** * @var */ protected $field_name; /** * @var string */ protected $destination_dir; /** * @var array */ protected $allow_mime; /** * @var array */ protected $allow_ext; /** * @var */ protected $file_org_name; /** * @var */ protected $file_type; /** * @var */ protected $file_tmp_name; /** * @var */ protected $file_error; /** * @var */ protected $file_size; /** * @var array */ protected $errors; /** * @var */ protected $extension; /** * @var */ protected $file_new_name; /** * @var float|int */ protected $allow_size; /** * UploadFile constructor. * @param $keyName * @param string $destinationDir * @param array $allowMime * @param array $allowExt * @param float|int $allowSize */ public function __construct($keyName, $destinationDir = './uploads', $allowMime = ['image/jpeg', 'image/gif'], $allowExt = ['gif', 'jpeg'], $allowSize = 2*1024*1024) { $this->field_name = $keyName; $this->destination_dir = $destinationDir; $this->allow_mime = $allowMime; $this->allow_ext = $allowExt; $this->allow_size = $allowSize; } /** * @param $destinationDir */ public function setDestinationDir($destinationDir) { $this->destination_dir = $destinationDir; } /** * @param $allowMime */ public function setAllowMime($allowMime) { $this->allow_mime = $allowMime; } /** * @param $allowExt */ public function setAllowExt($allowExt) { $this->allow_ext = $allowExt; } /** * @param $allowSize */ public function setAllowSize($allowSize) { $this->allow_size = $allowSize; } /** * @return bool */ public function upload() { // 判斷是否為多文件上傳 $files = []; if (is_array($_FILES[$this->field_name]['name'])) { foreach($_FILES[$this->field_name]['name'] as $k => $v) { $files[$k]['name'] = $v; $files[$k]['type'] = $_FILES[$this->field_name]['type'][$k]; $files[$k]['tmp_name'] = $_FILES[$this->field_name]['tmp_name'][$k]; $files[$k]['error'] = $_FILES[$this->field_name]['error'][$k]; $files[$k]['size'] = $_FILES[$this->field_name]['size'][$k]; } } else { $files[] = $_FILES[$this->field_name]; } foreach($files as $key => $file) { // 接收$_FILES參數 $this->setFileInfo($key, $file); // 檢查錯誤 $this->checkError($key); // 檢查MIME類型 $this->checkMime($key); // 檢查擴展名 $this->checkExt($key); // 檢查文件大小 $this->checkSize($key); // 生成新的文件名稱 $this->generateNewName($key); if (count((array)$this->getError($key)) > 0) { continue; } // 移動文件 $this->moveFile($key); } if (count((array)$this->errors) > 0) { return false; } return true; } /** * @return array */ public function getErrors() { return $this->errors; } /** * @param $key * @return mixed */ protected function getError($key) { return $this->errors[$key]; } /** * */ protected function setFileInfo($key, $file) { // $_FILES name type temp_name error size $this->file_org_name[$key] = $file['name']; $this->file_type[$key] = $file['type']; $this->file_tmp_name[$key] = $file['tmp_name']; $this->file_error[$key] = $file['error']; $this->file_size[$key] = $file['size']; } /** * @param $key * @param $error */ protected function setError($key, $error) { $this->errors[$key][] = $error; } /** * @param $key * @return bool */ protected function checkError($key) { if ($this->file_error > UPLOAD_ERR_OK) { switch($this->file_error) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: case UPLOAD_ERR_PARTIAL: case UPLOAD_ERR_NO_FILE: case UPLOAD_ERR_NO_TMP_DIR: case UPLOAD_ERR_CANT_WRITE: case UPLOAD_ERR_EXTENSION: $this->setError($key, self::UPLOAD_ERROR[$this->file_error]); return false; } } return true; } /** * @param $key * @return bool */ protected function checkMime($key) { if (!in_array($this->file_type[$key], $this->allow_mime)) { $this->setError($key, '文件類型' . $this->file_type[$key] . '不被允許!'); return false; } return true; } /** * @param $key * @return bool */ protected function checkExt($key) { $this->extension[$key] = pathinfo($this->file_org_name[$key],