Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<?php
// *单文件上传
printf('<pre>%s</pre>',print_r($_FILES,true));
if (isset($_FILES['my_pic'])) {
// *原始文件名
$name = $_FILES['my_pic']['name'];
// *临时文件名
$tmpName = $_FILES['my_pic']['tmp_name'];
//* 错误代码
$error = $_FILES['my_pic']['error'];
if ($error >0) {
$tips = '<span style="color:red">上传失败!</span><br>';
switch ($error) {
case 1:
$tips .= '大小超过了php.ini中的允许上传的文件大小';
break;
case 2:
$tips .= '大小超过了表单中(MAX_FILE_SIZE)的允许上传的文件大小';
break;
case 3:
$tips .= '文件只有部分被上传';
break;
case 4:
$tips .= '没有文件被上传';
break;
case 6:
$tips .= '找不到临时目录';
break;
case 7:
$tips .= '文件写入失败,请检查目录权限';
break;
}
echo "<p>$tips</p>";
} else {
//*1 判断上传方式是否合法? POST
if (is_uploaded_file($tmpName)) {
//* 文件类型白名单
$allow = ['jpg','jpeg','png','gif','JPG','JPEG','PNG','GIF'];
// *采用pathinfo('xxx.xx')就可得到其后缀名'.xx'
//*后缀名$ext
$ext = pathinfo($name)['extension'];
// *in_array(x,y):判断某一个值x,是否在数组y中
if (in_array($ext, $allow)) {
// *如果上传方式合法,且是允许的类型,则可以移动到指定的目录中了
$path = 'uploads/';
// *为了防止同名覆盖,应该给目标文件名重命名
// *其中md5(x)是对其加密
$dest =$path. md5($name) . '.' . $ext;
// *将文件从临时目录移动到目标目录中
if (move_uploaded_file($tmpName, $dest)) {
echo '<p>上传成功</p>';
//* 预览
echo "<img src='$dest' width='300'>";
} else {
echo '<p>上传不成功</p>';
}
} else {
echo '<p>文件类型错误</p>';
}
} else {
echo '<p>上传方式非法</p>';
}
// echo '上传成功了,进行下一步操作';
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<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>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
<input type="file" name="my_pic">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
<?php
// *多文件上传-逐个上传
printf('<pre>%s</pre>', print_r($_FILES, true));
foreach ($_FILES as $file) {
//* 只要判断$file['error'] === 0 表示上传成功
if ($file['error'] === 0) {
// *暂不加密目标文件名
$dest = 'uploads/' . $file['name'];
// *移动到目标目录中
move_uploaded_file($file['tmp_name'], $dest) ;
// *预览
echo "<img src='$dest' width='200'>";
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<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>多文件上传-1</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传-1:逐个上传</legend>
<input type="file" name="my_pic1">
<input type="file" name="my_pic2">
<input type="file" name="my_pic3">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
<?php
// *多文件上传-逐个上传采用二维数组形式
//* $_FILES
printf('<pre>%s</pre>', print_r($_FILES, true));
// *判断服务器中超全局变量$_FILES中的my_pic数组是否存在
if (isset($_FILES['my_pic'])) {
// *遍历二维数组中的[error]键
foreach ($_FILES['my_pic']['error'] as $key=>$error) {
//* 0 => UPLOAD_ERR_OK
// *如果error键的值为0
if ($error === UPLOAD_ERR_OK) {
// *临时文件名$tmpName
// *$key:键变量,分别代表[0],[1],[2]
$tmpName = $_FILES['my_pic']['tmp_name'][$key];
//* 原始文件名
$name = $_FILES['my_pic']['name'][$key];
// *暂不加密目标文件名
$dest = 'uploads/' . $name;
// *移动到目标目录中
move_uploaded_file($tmpName, $dest) ;
// *预览
echo "<img src='$dest' width='200'>";
}
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<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>多文件上传-2</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传-2:逐个上传</legend>
<input type="file" name="my_pic[]">
<input type="file" name="my_pic[]">
<input type="file" name="my_pic[]">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
<?php
// *多文件上传-批量上传
//* $_FILES
printf('<pre>%s</pre>', print_r($_FILES, true));
// *判断服务器中超全局变量$_FILES中的my_pic数组是否存在
if (isset($_FILES['my_pic'])) {
// *遍历二维数组中的[error]键
foreach ($_FILES['my_pic']['error'] as $key=>$error) {
//* 0 => UPLOAD_ERR_OK
// *如果error键的值为0
if ($error === UPLOAD_ERR_OK) {
// *临时文件名$tmpName
// *$key:键变量,分别代表[0],[1],[2]
$tmpName = $_FILES['my_pic']['tmp_name'][$key];
//* 原始文件名
$name = $_FILES['my_pic']['name'][$key];
// *暂不加密目标文件名
$dest = 'uploads/' . $name;
// *移动到目标目录中
move_uploaded_file($tmpName, $dest) ;
// *预览
echo "<img src='$dest' width='200'>";
}
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<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>多文件上传-2</title>
</head>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件上传-3:批量上传</legend>
<!-- multiple: 允许同时选择多个 -->
<input type="file" name="my_pic[]" multiple>
<button>上传</button>
<!-- 与demo3.php相比,只改了二个地方 -->
<!-- 1. 只保留一个my_pic[] -->
<!-- 2. 加一个 multiple 属性,支持多选 -->
<!-- php处理上传的代码和demo3.php完全一样,不用动 -->
</fieldset>
</form>
</body>
</html>