1. 单文件上传
<?php
// 可以将判断的信息异常捕获放入try...catch...模块中
echo '<pre>'.print_r($_FILES,true).'</pre>';
// 1. 自定义上传异常类(继承系统的Exception)
class UploadException extends Exception
{
public function __toString()
{
// 将捕获的异常信息放入表格中
return <<< ERROR
<table>
<tr>
<td>异常编号</td>
<td>异常信息</td>
<td>异常文件</td>
<td>异常行数</td>
</tr>
<tr>
<td>$this->code</td>
<td>$this->message</td>
<td>$this->file</td>
<td>$this->line</td>
</tr>
</table>
<style>
table{border-collapse: collapse;border: 1px solid black;text-align: center;}
td{padding: 8px;border: 1px solid black;}
tr:first-of-type{background-color: gray;color: white;}
tr:last-of-type{color: red;}
</style>
ERROR;
}
}
try {
// 2. 判断错误代码值,抛出相应的异常信息
$errorCode = $_FILES['file_name']['error'] ?? null;
if ($errorCode > UPLOAD_ERR_OK) {
switch ($errorCode) {
case UPLOAD_ERR_INI_SIZE :
throw new UploadException('文件超过`php.ini`中`upload_max_filesize`值',1);
break;
case UPLOAD_ERR_FORM_SIZE :
throw new UploadException('文件大小超过表单中`MAX_FILE_SIZE`指定的值',2);
break;
case UPLOAD_ERR_PARTIAL :
throw new UploadException('文件只有部分被上传',3);
break;
case UPLOAD_ERR_NO_FILE :
throw new UploadException('没有文件被上传',4);
break;
case UPLOAD_ERR_NO_TMP_DIR :
throw new UploadException('找不到临时文件夹',6);
break;
default:
throw new UploadException("未知错误,上传失败", 7);
}
}
// 3. 判断临时文件上传方式是否正确,抛出相应异常信息
$tempFileName = $_FILES['file_name']['tmp_name'] ?? null;
// 如果不是POST方式上传,抛出异常
if($_POST['btn'] === 1){
if (!($tempFileName && is_uploaded_file($tempFileName))){
throw new UploadException('上传方式有误',8);
}
}
// 如果上传方式正确,将临时文件储存到uploads目录下
if ($tempFileName && is_uploaded_file($tempFileName)){
$originalFileName = $_FILES['file_name']['name'] ?? null;
$destination = 'uploads/'.md5(time()).mt_rand(1,10000).strstr($originalFileName,".");
if(move_uploaded_file($tempFileName,$destination)){
echo "<br>临时文件储存在: ".$tempFileName;
echo "<br>现在文件被移动到: ".$destination;
echo "<br>文件上传成功";
echo "<br><img src='{$destination}' width='180'>";
}
}
// 4. 判断文件类型是否正确,抛出相应异常信息
$fileType = $_FILES['file_name']['type'] ?? null;
$endType = strstr($fileType,"/",true);
if (!is_null($fileType)){
if($endType !== "image")
throw new UploadException('上传文件格式错误,请上传image类型的图片',9);
}
} catch (UploadException $e) {
echo $e;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>文件上传异常处理</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传</legend>
<!-- 设置前端上传文件的大小 -->
<input type="hidden" name="MAX_FILE_SIZE" value="380000">
<input type="file" name="file_name" />
<button name="btn" value="1">上传</button>
</fieldset>
</form>
</body>
</html>
<?php
error_reporting(E_ALL);
echo '<pre>'.print_r($_FILES,true).'</pre>';
// 1. 自定义异常
class UploadException extends Exception
{
public function __toString()
{
// 将捕获的异常信息放入表格中
return <<< ERROR
<table>
<tr>
<td>异常编号</td>
<td>异常信息</td>
<td>异常文件</td>
<td>异常行数</td>
</tr>
<tr>
<td>$this->code</td>
<td>$this->message</td>
<td>$this->file</td>
<td>$this->line</td>
</tr>
</table>
<style>
table{border-collapse: collapse;border: 1px solid black;text-align: center;}
td{padding: 8px;border: 1px solid black;}
tr:first-of-type{background-color: gray;color: white;}
tr:last-of-type{color: red;}
</style>
ERROR;
}
}
try {
// 多文件上传时,先遍历$_FILES
foreach($_FILES as $file){
// 2. 判断错误代码值,抛出相应的异常信息
$errorCode = $file['error'] ?? null;
if ($errorCode > UPLOAD_ERR_OK) {
switch ($errorCode) {
case UPLOAD_ERR_INI_SIZE :
throw new UploadException('文件超过`php.ini`中`upload_max_filesize`值',1);
break;
case UPLOAD_ERR_FORM_SIZE :
throw new UploadException('文件大小超过表单中`MAX_FILE_SIZE`指定的值',2);
break;
case UPLOAD_ERR_PARTIAL :
throw new UploadException('文件只有部分被上传',3);
break;
case UPLOAD_ERR_NO_FILE :
throw new UploadException('没有文件被上传',4);
break;
case UPLOAD_ERR_NO_TMP_DIR :
throw new UploadException('找不到临时文件夹',6);
break;
default:
throw new UploadException("未知错误,上传失败", 7);
}
}
// 3. 判断临时文件上传方式是否正确,抛出相应异常信息
$tmpFileName = $file['tmp_name'] ?? null;
// 如果不是POST方式上传,抛出异常
if($_POST['btn'] === 1){
if (!($tmpFileName && is_uploaded_file($tmpFileName))){
throw new UploadException('上传方式有误',8);
}
}
// 如果上传方式正确,将临时文件储存到uploads目录下
if ($tmpFileName && is_uploaded_file($tmpFileName)){
$originalFileName = $file['name'] ?? null;
$destination = 'uploads/'.md5(time()).mt_rand(1,10000).strstr($originalFileName,".");
if(move_uploaded_file($tmpFileName,$destination)){
echo "<img src='{$destination}' width='180'>";
echo "<span style='color:red;font-size:1.5rem;'>{$originalFileName} 上传成功 </span><hr>";
}
}
// 4. 判断文件类型是否正确,抛出相应异常信息
$fileType = $file['type'] ?? null;
$endType = strstr($fileType,"/",true);
if (!is_null($fileType)){
if($endType !== "image")
throw new UploadException('上传文件格式错误,请上传image类型的图片',9);
}
}
} catch (UploadException $e) {
echo $e;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>多文件上传</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>批量上传文件</legend>
<!-- 隐藏域设置上传文件大小,必须写到input:file之前 -->
<!-- <input type="hidden" name="MAX_FILE_SIZE" value="55000"> -->
<input type="file" name="file1" > <br>
<input type="file" name="file2" > <br>
<input type="file" name="file3" >
<button name="btn" value="1">上传</button>
</fieldset>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>多文件上传批量处理</title>
</head>
<body>
<form action="check.php" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传</legend>
<!-- 设置前端上传文件的大小 -->
<!-- <input type="hidden" name="MAX_FILE_SIZE" value="380000"> -->
<input type="file" name="file[]" multiple />
<button name="btn" value="1">上传</button>
</fieldset>
</form>
</body>
</html>
<?php
// 可以将判断的信息异常捕获放入try...catch...模块中
// 测试数据显示的代码,根据测试才有了以下的写法
// echo '<pre>'.print_r($_FILES['file'],true).'</pre>';
// print_r($_FILES['file']['name']);
// print_r($_FILES['file']['type']);
// print_r($names)
// echo $names[0];
// print_r( $types);
// 将三维数组全部转为一维数组,将$_FILES的键值直接保存下来,后期直接遍历相应的数组就可以取出值来
$names = $_FILES['file']['name'];
$types = $_FILES['file']['type'];
$tmp_names = $_FILES['file']['tmp_name'];
$errors = $_FILES['file']['error'];
$sizes = $_FILES['file']['size'];
// 1. 自定义上传异常类(继承系统的Exception)
class UploadException extends Exception
{
public function __toString()
{
// 将捕获的异常信息放入表格中
return <<< ERROR
<table>
<tr>
<td>异常编号</td>
<td>异常信息</td>
<td>异常文件</td>
<td>异常行数</td>
</tr>
<tr>
<td>$this->code</td>
<td>$this->message</td>
<td>$this->file</td>
<td>$this->line</td>
</tr>
</table>
<style>
table{border-collapse: collapse;border: 1px solid black;text-align: center;}
td{padding: 8px;border: 1px solid black;}
tr:first-of-type{background-color: gray;color: white;}
tr:last-of-type{color: red;}
</style>
ERROR;
}
}
try {
// 2. 判断错误代码值,抛出相应的异常信息
foreach($errors as $errorCode){
if ($errorCode > UPLOAD_ERR_OK) {
switch ($errorCode) {
case UPLOAD_ERR_INI_SIZE :
throw new UploadException('文件超过`php.ini`中`upload_max_filesize`值',1);
break;
case UPLOAD_ERR_FORM_SIZE :
throw new UploadException('文件大小超过表单中`MAX_FILE_SIZE`指定的值',2);
break;
case UPLOAD_ERR_PARTIAL :
throw new UploadException('文件只有部分被上传',3);
break;
case UPLOAD_ERR_NO_FILE :
throw new UploadException('没有文件被上传',4);
break;
case UPLOAD_ERR_NO_TMP_DIR :
throw new UploadException('找不到临时文件夹',6);
break;
default:
throw new UploadException("未知错误,上传失败", 7);
}
}
}
// 3. 判断临时文件上传方式是否正确,抛出相应异常信息
foreach($tmp_names as $index => $tmpFileName){
// 如果不是POST方式上传,抛出异常
if($_POST['btn'] === 1){
if (!($tmpFileName && is_uploaded_file($tmpFileName))){
throw new UploadException('上传方式有误',8);
}
}
// 如果上传方式正确,将临时文件储存到uploads目录下
// 上面遍历的临时文件名中$index的作用就在这里,根据$tmp_names[$index]就可以取到$names[$index]
// 因为它们的键名都是相对应的,所以可以通过$index拿到文件名的后缀
if ($tmpFileName && is_uploaded_file($tmpFileName)){
$destination = 'uploads/'.md5(time()).mt_rand(1,10000).strstr($names[$index],".");
if(move_uploaded_file($tmpFileName,$destination)){
echo "<img src='{$destination}' width='180'>";
echo "<span style='color:red;font-size:1.5rem;'> {$names[$index]}上传成功 </span><hr>";
}
}
}
// 4. 判断文件类型是否正确,抛出相应异常信息
foreach($types as $filetype){
$endType = strstr($fileType,"/",true);
if (!is_null($fileType)){
if($endType !== "image")
throw new UploadException('上传文件格式错误,请上传image类型的图片',9);
}
}
} catch (UploadException $e) {
echo $e;
}
?>
4. 封装一个类,支持单元件与多文件上传
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>多文件上传批量处理</title>
</head>
<body>
<form action="check.php" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传</legend>
<!-- 设置前端上传文件的大小 -->
<!-- <input type="hidden" name="MAX_FILE_SIZE" value="380000"> -->
<input type="file" name="file[]" multiple />
<button name="btn" value="1">上传</button>
</fieldset>
</form>
</body>
</html>
<?php
// 1. 自定义上传异常类(继承系统的Exception)
class UploadException extends Exception
{
public function __toString()
{
// 将捕获的异常信息放入表格中
return <<< ERROR
<table>
<tr>
<td>异常编号</td>
<td>异常信息</td>
<td>异常文件</td>
<td>异常行数</td>
</tr>
<tr>
<td>$this->code</td>
<td>$this->message</td>
<td>$this->file</td>
<td>$this->line</td>
</tr>
</table>
<style>
table{border-collapse: collapse;border: 1px solid black;text-align: center;}
td{padding: 8px;border: 1px solid black;}
tr:first-of-type{background-color: gray;color: white;}
tr:last-of-type{color: red;}
</style>
ERROR;
}
}
class UploadFile extends UploadException
{
public $names;
public $types;
public $tmp_names;
public $errors;
public $sizes;
public function __construct($types,$tmp_names,$errors,$names,$sizes)
{
$this->names = $names;
$this->types = $types;
$this->tmp_names = $tmp_names;
$this->errors = $errors;
$this->sizes = $sizes;
return $this->check();
}
public function check (){
try {
// 2. 判断错误代码值,抛出相应的异常信息
foreach($this->errors as $errorCode){
if ($errorCode > UPLOAD_ERR_OK) {
switch ($errorCode) {
case UPLOAD_ERR_INI_SIZE :
throw new UploadException('文件超过`php.ini`中`upload_max_filesize`值',1);
break;
case UPLOAD_ERR_FORM_SIZE :
throw new UploadException('文件大小超过表单中`MAX_FILE_SIZE`指定的值',2);
break;
case UPLOAD_ERR_PARTIAL :
throw new UploadException('文件只有部分被上传',3);
break;
case UPLOAD_ERR_NO_FILE :
throw new UploadException('没有文件被上传',4);
break;
case UPLOAD_ERR_NO_TMP_DIR :
throw new UploadException('找不到临时文件夹',6);
break;
default:
throw new UploadException("未知错误,上传失败", 7);
}
}
}
// 3. 判断临时文件上传方式是否正确,抛出相应异常信息
foreach($this->tmp_names as $index => $tmpFileName){
// 如果不是POST方式上传,抛出异常
if($_POST['btn'] === 1){
if (!($tmpFileName && is_uploaded_file($tmpFileName))){
throw new UploadException('上传方式有误',8);
}
}
// 如果上传方式正确,将临时文件储存到uploads目录下
// 上面遍历的临时文件名中$index的作用就在这里,根据$tmp_names[$index]就可以取到$names[$index]
// 因为它们的键名都是相对应的,所以可以通过$index拿到文件名的后缀
if ($tmpFileName && is_uploaded_file($tmpFileName)){
$destination = 'uploads/'.md5(time()).mt_rand(1,10000).strstr($this->names[$index],".");
if(move_uploaded_file($tmpFileName,$destination)){
echo "<img src='{$destination}' width='180'>";
echo "<span style='color:red;font-size:1.5rem;'> {$names[$index]}上传成功 </span><hr>";
}
}
}
// 4. 判断文件类型是否正确,抛出相应异常信息
foreach($this->types as $filetype){
$endType = strstr($fileType,"/",true);
if (!is_null($fileType)){
if($endType !== "image")
throw new UploadException('上传文件格式错误,请上传image类型的图片',9);
}
}
} catch (UploadException $e) {
echo $e;
}
}
}
?>
<?php
spl_autoload_register('meth');
function meth ($class){
$file = __DIR__.'/'.$class.'.php';
require_once $file;
}
$types = $_FILES['file']['type'] ?? null;
$tmp_names = $_FILES['file']['tmp_name'] ?? null;
$errors = $_FILES['file']['error'] ?? null;
$sizes = $_FILES['file']['size'] ?? null;
$names = $_FILES['file']['name'] ?? null;
$obj = new UploadFile($types,$tmp_names,$errors,$names,$sizes);
?>
5. 总结