Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:文件上传, 在任何语言中都是一个重点, 也是难点, 概念不难, 难在流程, 要记住并完全理解有难度
<?php
require 'uploads.php';
(new Uploads)->upload('pic', 'uplosd');
?>
<!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_FILES_SIZE" value="500000">
<input type="file" name="pic[]" id="file" multiple>
<button>上传</button>
</fieldset>
</form>
</body>
</html>
<?php
class Uploads
{
protected $filesname;
public function upload($name, $past){
$this->filesname = $name;
// 判断上传的文件是否成功
if($_FILES[ $this->filesname]){
foreach($_FILES[$this->filesname]['error'] as $key=>$value){
if($value === 0){
// 临时文件名
$tmpFileName = $_FILES[$this->filesname]['tmp_name'][$key];
// 原始文件名
$originalFileName =$_FILES[$this->filesname]['name'][$key];
// 目标文件名
$destFileName = $past.md5($originalFileName);
// 移动文件:第一个参数:要移动的文件。第二个参数:目标文件名
move_uploaded_file($tmpFileName,$destFileName);
// 预览
echo "<img src='{$destFileName}' width='100'>";
echo "文件上传成功";
}else{
echo '文件上传失败';
}
}
}
}
}
?>