Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
if(isset($_FILES['userfile'])){
$error = $_FILES['userfile']['error'];
// var_dump($error);
if($error){
switch($error){
case 1: echo "上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值.";
break;
case 2: echo "上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值.";
break;
case 3: echo "文件只有部分被上传。";
break;
case 4: echo "没有文件被上传。";
break;
case 6: echo "找不到临时文件夹。";
break;
case 7: echo "文件写入失败。";
default: echo "未知错误";
}
}else{
$name =pathinfo($_FILES['userfile']['name'])['filename'];
$ext = pathinfo($_FILES['userfile']['name'])['extension'];
$newFileName = 'upload/'.md5($name).'.'.$ext;
// var_dump($newFileName,$_FILES['userfile']['tmp_name']);
if(move_uploaded_file($_FILES['userfile']['tmp_name'],"$newFileName")){
echo "上传成功";
}else{
echo '123';
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传功能</title>
</head>
<body>
<h1>单文件上传功能</h1>
<form action="demo.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1048576">
<input type="file" name="userfile" id="">
<button>上传</button>
</form>
</body>
</html>
<?php
// var_dump($_FILES);
$message = "";
foreach($_FILES['userfile']['error'] as $key => $value){
if(!$value){
switch($value){
case 1: $message .="第{$key}个文件上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。"."<br>";
break;
case 2: $message .="第{$key}个文件上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值。"."<br>";
break;
case 3: $message .="第{$key}个文件文件只有部分被上传。"."<br>";
break;
case 4: $message .="第{$key}个文件没有文件被上传。"."<br>";
break;
case 6: $message .="第{$key}个文件找不到临时文件夹。"."<br>";
break;
case 7: $message .="第{$key}个文件文件写入失败。"."<br>";
// default: echo "未知错误";
}
}
}
if($message){
echo $message;
exit;
}
foreach($_FILES['userfile']['error'] as $key => $value){
if(is_uploaded_file($_FILES['userfile']['tmp_name'][$key])){
$newFileName = 'upload/'.$_FILES['userfile']['name'][$key];
$oldFileName = $_FILES['userfile']['tmp_name'][$key];
move_uploaded_file($oldFileName,$newFileName);
echo "上传成功";
}else{
echo "未知错误";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>文件上传功能</title>
</head>
<body>
<h1>多文件上传功能</h1>
<form action="demo.php" method="POST" enctype="multipart/form-data">
<!-- <input type="hidden" name="MAX_FILE_SIZE" value="1048576"> -->
<input type="file" name="userfile[]" id="" multiple>
<button>上传</button>
</form>
</body>
</html>