Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:挺好 的
<?php
// 自定文件上传异常处理类
class UploadException extends Exception
{
public function __toString()
{
return <<< UPLOAD
<style>
table {text-align:center;border:1px solid black;border-collapse:collapse;}
th,td {padding:5px;border:1px solid black;}
tr:hover {background-color:rgb(240,240,240);}
td {color:coral;}
</style>
<table>
<tr>
<th>编号</th>
<th>信息</th>
<th>文件</th>
<th>行号</th>
</tr>
<tr>
<td>$this->code</td>
<td>$this->message</td>
<td>$this->file</td>
<td>$this->line</td>
</tr>
</table>
UPLOAD;
}
}
// 捕捉异常
try {
// 对错误编码的判断
$errorCode = $_FILES['img']['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;
case UPLOAD_ERR_CANT_WRITE:
throw new UploadException('文件写入失败', 7);
break;
default:
throw new UploadException('未知错误', 8);
}
}
// 文件类型检查
// 原始文件名
$fileType = $_FILES['img']['type'] ?? null;
// 切割字符串strstr(),获取文件类型
$type = strstr($fileType, '/', true);
if (!is_null($fileType)) {
if ($type !== 'image') {
throw new UploadException('文件类型错误', 9);
}
}
// 上传方式检查
// 临时文件名
$tmpFileName = $_FILES['img']['tmp_name'] ?? null;
// 临时文件是存在的,并且是POST上传
if ($tmpFileName && is_uploaded_file($tmpFileName)) {
// 将文件从临时目录移到目标目录move_uploaded_file(临时文件名,目标文件名)
// 原始文件名
$originalFileName = $_FILES['img']['name'] ?? null;
// 文件扩展名
$extension = strstr($originalFileName, '.');
// 目标文件名加密
$destFileName = 'uploads/' . md5(time() . mt_rand(1, 1000)) . strstr($originalFileName, '.');
if (move_uploaded_file($tmpFileName, $destFileName)) {
echo '<p>' . $_FILES['img']['name'] . ':文件上传成功' . '</p>';
// 上传成功后预览
echo "<img src='{$destFileName}' width='200'>";
}
}
} catch (UploadException $e) {
echo $e;
}
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传成功后从临时目录移到目标目录</title>
</head>
<body>
<!-- 上传文件必须的表单属性:method="POST" enctype="multipart/form-data" -->
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传</legend>
<!-- 隐藏域设置上传文件大小必须写到文件上传标签前面 -->
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="file" name="img" id="">
<button type="submit">提交上传</button>
</fieldset>
</form>
</body>
运行效果:
上传成功:
<?php
// 自定文件上传异常处理类
class UploadException extends Exception
{
public function __toString()
{
return <<< UPLOAD
<style>
table {text-align:center;border:1px solid black;border-collapse:collapse;}
th,td {padding:5px;border:1px solid black;}
tr:hover {background-color:rgb(240,240,240);}
td {color:coral;}
</style>
<table>
<tr>
<th>编号</th>
<th>信息</th>
<th>文件</th>
<th>行号</th>
</tr>
<tr>
<td>$this->code</td>
<td>$this->message</td>
<td>$this->file</td>
<td>$this->line</td>
</tr>
</table>
UPLOAD;
}
}
// 捕获异常
try {
// 遍历上传文件
foreach ($_FILES as $file) {
// print_r($file);
// 对错误编码的判断
$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;
case UPLOAD_ERR_CANT_WRITE:
throw new UploadException('文件写入失败', 7);
break;
default:
throw new UploadException('未知错误', 8);
}
}
// 文件类型检查
// 原始文件名
$fileType = $file['type'] ?? null;
// 切割字符串strstr(),获取文件类型
$type = strstr($fileType, '/', true);
if (!is_null($fileType)) {
if ($type !== 'image') {
throw new UploadException('文件类型错误', 9);
}
}
// 上传方式检查
// 临时文件名
$tmpFileName = $file['tmp_name'] ?? null;
// 临时文件是存在的,并且是POST上传
if ($tmpFileName && is_uploaded_file($tmpFileName)) {
// 将文件从临时目录移到目标目录move_uploaded_file(临时文件名,目标文件名)
// 原始文件名
$originalFileName = $file['name'] ?? null;
// 文件扩展名
$extension = strstr($originalFileName, '.');
// 目标文件名加密
$destFileName = 'uploads/' . md5(time() . mt_rand(1, 1000)) . strstr($file['name'], '.');
if (move_uploaded_file($tmpFileName, $destFileName)) {
echo '<p>' . $originalFileName . ':文件上传成功' . '</p>';
// 上传成功后预览
echo "<img src='{$destFileName}' width='200'>";
}
}
}
} catch (UploadException $e) {
echo $e;
}
?>
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>多文件上传</title>
</head>
<body>
<!-- 上传文件必须的表单属性:method="POST" enctype="multipart/form-data" -->
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传</legend>
<!-- 隐藏域设置上传文件大小必须写到文件上传标签前面 -->
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<!-- 多文件的name属性值不能一样 -->
<input type="file" name="img1" id="">
<input type="file" name="img2" id="">
<input type="file" name="img3" id="">
<hr>
<button type="submit">提交上传</button>
</fieldset>
</form>
</body>
运行效果
上传成功(必须全部上传成功):
1.前端隐藏域限制文件大小要放在上传表单前面;
2.后端也要验证文件大小及类型;
3.多文件上传的表单 name
属性值不能一样;
4.文件上传要用 POST
方式;
5.上传成功后的文件名要用加密方式区别开,以防同名文件重复上传覆盖。