Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<!DOCTYPE html>
<html lang="en">
<?php
printf('<pre>%s</pre>', print_r($_FILES, true));
foreach ($_FILES as $file) {
extract($file);
if ($error > 0) {
$tips = '<span style="color:red">上传失败:</span>';
switch ($error) {
case '1':
$tips .= '文件超过系统设置的最大大小';
break;
case '2':
$tips .= '文件超过表单设置的最大大小';
break;
case '3':
$tips .= '文件部分被上传';
break;
case '4':
$tips .= '无文件上传';
break;
case '6':
$tips .= '找不到临时目录';
break;
case '7':
$tips .= '文件写入失败';
break;
}
echo $tips;
} else {
//判断指定的文件是否是通过 HTTP POST 上传的
if (is_uploaded_file($tmp_name)) {
//设置允许上传的文件类型
$allow = ['jpg', 'jpeg', 'png', 'bmp'];
//获取文件的扩展名
$ext = pathinfo($name)['extension'];
//判断文件类型是够合法
if (in_array($ext, $allow)) {
//目标路径
$path = 'upload/';
//自定义目标文件名
$dest = $path . md5($name) . '.' . $ext;
//将文件从临时目录中移动到目标目录中并重命名
if (move_uploaded_file($tmp_name, $dest)) {
echo '<span style="color:red">上传成功<br></span>';
//预览
echo "<img src='$dest' width='200px'></img><br>";
} else echo '<span style="color:red">移动失败</span>';
} else echo '<span style="color:red">非法文件类型</span>';
} else echo '<span style="color:red">非法方式上传</span>';
}
}
?>
<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>
<!-- 允许上传文件的表单:
1.method = "POST";
2.enctype = "multipart/form-data" 规定在提交表单时要需要的内容类型
-->
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传</legend>
<input type="file" name="my_pic">
<button>上传</button>
</fieldset>
</form>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件文件上传-逐个上传</legend>
<input type="file" name="my_pic">
<input type="file" name="my_pic1">
<input type="file" name="my_pic2">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<?php
printf('<pre>%s</pre>', print_r($_FILES, true));
if (isset($_FILES['my_pic'])) {
//遍历$_FILES['my_pic']['error']数组,值>0
$errorArray = $_FILES['my_pic']['error'];
foreach ($errorArray as $key => $error) {
$tmp_name = $_FILES['my_pic']['tmp_name'][$key];
$name = $_FILES['my_pic']['name'][$key];
if ($error > 0) {
$tips = '<span style="color:red">上传失败:</span>';
switch ($error) {
case '1':
$tips .= '文件超过系统设置的最大大小';
break;
case '2':
$tips .= '文件超过表单设置的最大大小';
break;
case '3':
$tips .= '文件部分被上传';
break;
case '4':
$tips .= '无文件上传';
break;
case '6':
$tips .= '找不到临时目录';
break;
case '7':
$tips .= '文件写入失败';
break;
}
echo $tips;
} else {
//判断指定的文件是否是通过 HTTP POST 上传的
if (is_uploaded_file($tmp_name)) {
//设置允许上传的文件类型
$allow = ['jpg', 'jpeg', 'png', 'bmp'];
//获取文件的扩展名
$ext = pathinfo($name)['extension'];
//判断文件类型是够合法
if (in_array($ext, $allow)) {
//目标路径
$path = 'upload/';
//自定义目标文件名
$dest = $path . md5($name) . '.' . $ext;
//将文件从临时目录中移动到目标目录中并重命名
if (move_uploaded_file($tmp_name, $dest)) {
echo '<span style="color:red">上传成功<br></span>';
//预览
echo "<img src='$dest' width='200px'></img><br>";
} else echo '<span style="color:red">移动失败</span>';
} else echo '<span style="color:red">非法文件类型</span>';
} else echo '<span style="color:red">非法方式上传</span>';
}
};
} else echo '<span style="color:red">上传失败</span>';
?>
<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>
<!-- 允许上传文件的表单:
1.method = "POST";
2.enctype = "multipart/form-data" 规定在提交表单时要需要的内容类型
-->
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件文件上传-逐个上传v2</legend>
<!-- 使用数组表示每个要上传的表单域的名称 -->
<input type="file" name="my_pic[]">
<input type="file" name="my_pic[]">
<input type="file" name="my_pic[]">
<button>上传</button>
</fieldset>
</form>
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>多文件文件上传-多个上传</legend>
<!-- multiple:允许同时选择多个 -->
<input type="file" name="my_pic[]" multiple>
<button>上传</button>
</fieldset>
</form>
</body>
</html>
<?php
//连接数据库
$db = new PDO('mysql:dbname=phpedu', 'root', 'root');
//当前页
$page = $_GET['p'] ?? 1;
echo '当前页:' . $page . '<br>';
//每页显示数量
$num = 5;
//记录总数
$sql = 'SELECT COUNT(`id`) AS `total` FROM `staff`';
$stmt = $db->prepare($sql);
$stmt->execute();
$stmt->bindColumn('total', $total);
$stmt->fetch(PDO::FETCH_ASSOC);
echo '记录总数:' . $total . '<br>';
//总页数
$pages = ceil($total / $num);
echo '总页数:' . $pages . '<br>';
//每页的起始索引,偏移量
$offset = ($page - 1) * $num;
echo '偏移量:' . $offset . '<hr>';
//分页数据遍历
$sql = "SELECT * FROM `staff` LIMIT $num OFFSET $offset";
$stmt = $db->prepare($sql);
$stmt->execute();
$staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (count($staffs) === 0) {
echo '查询结果为空';
} else {
foreach ($staffs as $staff) {
extract($staff);
echo "$id-$name-$gender-$email<br>";
}
}
echo '<hr>';
?>
<!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>
<style>
table {
width: 400px;
text-align: center;
border-collapse: collapse;
}
table th,
table td {
border: 1px solid black;
padding: 5px;
}
table caption {
font-size: larger;
margin-bottom: 8px;
}
table thead {
background-color: lightcyan;
}
p>a {
text-decoration: none;
color: black;
border: 1px solid black;
padding: 5px 10px;
margin: 10px 2px;
}
.active {
color: white;
background-color: seagreen;
border: 1px solid white;
}
</style>
<body>
<table>
<caption>员工信息表</caption>
<thead>
<tr>
<th>序号</th>
<th>姓名</th>
<th>性别</th>
<th>邮箱</th>
</tr>
</thead>
<tbody>
<?php foreach ($staffs as $staff) : extract($staff) ?>
<tr>
<td><?= $id ?></td>
<td><?= $name ?></td>
<td><?= ($gender === '1') ? '男' : '女' ?></td>
<td><?= $email ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<p>
<?php for ($i = 1; $i <= $pages; $i++) : ?>
<?php
//$_SERVER['PHP_SELF']返回当前执行脚本的文件名
$url = $_SERVER['PHP_SELF'] . '?p=' . $i;
//$i int类型;$page string类型,不能用===
$active = ($i == $page) ? 'active' : null;
?>
<a href="<?= $url ?>" class="<?= $active ?>"><?= $i ?></a>
<?php endfor ?>
</p>
</body>
</html>