Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:这个案例做完, 简单的逻辑业务封装, 应该难不倒你了, 恭喜你
-包括三个文件
1、文件上传页面。在该页面选择上传文件后,调用文件上传类
2、在文件上传类中,进行判断,上传成功且是图片文件类型的,保存文件并进行预览;否则调用错误类型判断类。
3、错误类型判断类,识别错误类型,列出错误信息。
效果图如下:
<?php
require_once "upload.php";
(new upload)->uploadfile('my_pic');
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传变量$_FILES</title>
<style>
*{
margin: 0;
padding:0
}
.container {
width:450px;
height:150px;
margin:50px auto;
}
</style>
</head>
<body>
<div class="container">
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="3000000">
<!-- 将name属性值能数组的形式提供 -->
<input type="file" name="my_pic[]" multiple>
<button>上传</button>
</fieldset>
</form>
</div>
</body>
</html>
<?php
require_once "error.php";
class upload{
protected $uploadFilename;
public function uploadfile($name){
$this->uploadFilename=$name;
if (!empty($_FILES[$this->uploadFilename])){
foreach ($_FILES[$this->uploadFilename]['error'] as $key => $error) {
$fileType=strstr($_FILES[$this->uploadFilename]['type'][$key], '/', true);
// 原始文件名
$originalFileName = $_FILES[$this->uploadFilename]['name'][$key];
if ($error === UPLOAD_ERR_OK && $fileType === 'image') {
// 临时文件名
$tmpFileName = $_FILES[$this->uploadFilename]['tmp_name'][$key];
// 目标文件名
$destFileName = 'uploads/'. md5(time().mt_rand(1,1000)).strstr($originalFileName, '.');
// 移动文件
move_uploaded_file($tmpFileName, $destFileName);
// 预览
echo "<img src='{$destFileName}' width='200'>","{$originalFileName} 上传成功!",'<br>';
}else
{
(new UploadException)->judgerror($error,$fileType);
echo "{$originalFileName} 上传失败!",'<br>';
}
}
}
}
}
?>
总结: