Correcting teacher:PHPz
Correction status:qualified
Teacher's comments:
<!DOCTYPE html>
<html lang="en">
<?php
//! $_FILES: PHP超全局变量数量, 保存着上传文件的全部信息
printf('<pre>%s</pre>', print_r($_FILES,true));
//!isset: 检测变量是否已声明并且其值不为 null
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:lightgreen">上传失败:</span>';
switch($error){
case 1 :
$tips .='上传的文件超过了php.ini 中upload_max_filesize 选项限制的值';
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{
//! is_uploaded_file:判断文件是否是通过 HTTP POST 上传的
if(is_uploaded_file($tmpName)){
$allow = ['jpg','jpeg','png','gif'];
//! pathinfo:返回文件路径的信息,
//!如果没有传入 options ,将会返回包括以下单元的数组 array:dirname,basename 和 extension
$ext = pathinfo($name)['extension'];
// print_r($ext);
//! in_array:检查数组中是否存在某个值
if(in_array($ext,$allow)){
// $path = 'C:\Users\Administrator\Desktop\后端学习质料\0427-1\up/';
$path = 'C:/Users/Administrator/Desktop/后端学习质料/0427/up/';
//!将目标文件名md5加密
$dest =$path . md5($name) . '.' . $ext;
//预览文件存放的地方
$show = 'up/'.md5($name). '.' . $ext;
// print_r($dest);
//!move_uploaded_file: 将上传的文件移动到新位置
if(move_uploaded_file($tmpName, $dest)){
// move_uploaded_file($tmpName, $path);
echo '<p style="color:red">上传成功</p>';
echo "<img src='$show' width='200'>";
}else{
echo '<p style="color:lightblue">移动失败</p>';
}
}else{
echo '<p style="color:lightblue">文件类型错误</p>';
}
}else{
echo '<p style="color:lightblue">非法方式上传</p>';
}
}
}
?>
<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>
<!-- enctype:规定了form表单在发送到服务器时候,数据的编码方式 -->
<!-- multipart/form-data: 指定传输数据为二进制类型,比如图片、mp3、文件。 -->
<form action="" method="POST" enctype="multipart/form-data">
<fieldset>
<legend>单文件上传</legend>
<!-- type="hidden":隐藏域 -->
<!-- MAX_FILE_SIZE,意思是接收文件的最大尺寸,与后面的value的值对应 -->
<input type="hidden" name="MAX_FILE_SIZE" value="300000">
<input type="file" name="my_pic">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<?php
printf('<pre>%s</pre>',print_r($_FILES,true));
foreach($_FILES as $file){
if($file['error']>0){
$tips = '<span style="color:lightgreen">上传失败:</span>';
switch($file['error']){
case 1 :
$tips .='上传的文件超过了php.ini 中upload_max_filesize 选项限制的值';
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{
if(is_uploaded_file($file['tmp_name'])){
$allow = ['jpg','jpeg','png','gif'];
//! pathinfo:返回文件路径的信息,
//!如果没有传入 options ,将会返回包括以下单元的数组 array:dirname,basename 和 extension
$ext = pathinfo($file['name'])['extension'];
// print_r($ext);
//! in_array:检查数组中是否存在某个值
if(in_array($ext,$allow)){
// $path = 'C:\Users\Administrator\Desktop\后端学习质料\0427-1\up/';
$path = 'C:/Users/Administrator/Desktop/后端学习质料/0427/ups/';
//!将目标文件名md5加密
$dest =$path . md5($file['name']) . '.' . $ext;
$show = 'ups/'.md5($file['name']). '.' . $ext;
// $dest =$name;
// print_r($dest);
//!move_uploaded_file: 将上传的文件移动到新位置
if(move_uploaded_file($file['tmp_name'], $dest)){
// move_uploaded_file($tmpName, $path);
echo '<p style="color:red">上传成功</p>';
echo "<img src='$show' width='200'>";
}else{
echo '<p style="color:lightblue">移动失败</p>';
}
}else{
echo '<p style="color:lightblue">文件类型错误</p>';
}
}else{
echo '<p style="color:lightblue">非法方式上传</p>';
}
}
}
?>
<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="file" name="my_pic1">
<input type="file" name="my_pic2">
<input type="file" name="my_pic3">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
<?php
//1.连接数据库
$db = new PDO('mysql:dbname=php','root','root');
//2.获取当前页,设置默认值为1.
$now= $_GET['p']?? 1;
//3.每页显示数量
$num = 5;
//4.获取数据库数据总条数
$sql = 'SELECT count(`id`) as `total` from `student`';
$stmt = $db ->prepare($sql);
$stmt -> execute();
//!bindColumn:绑定一列到一个 PHP 变量(类似于list()函数为变量赋值)
$stmt ->bindColumn('total',$total);
$stmt -> fetch(PDO::FETCH_ASSOC);
//5.总页数
//! ceil:向上取整
$pages= ceil($total / $num);
//6.偏移量
$off= ($now - 1) * $num;
//7.分页数据
//!查询分页
$sql = "SELECT * from `student` limit $num offset $off";
$stmt = $db ->prepare($sql);
$stmt -> execute();
//读取所有分页里的数据
$staffs = $stmt ->fetchAll(PDO::FETCH_ASSOC);
//遍历并输出
if(count($staffs)===0){
echo '查询结果为空';
}else{
foreach($staffs as staff){
extract($staff);
printf('<pre>%d,%s,%s,%s</pre',$id,$name,$sex,$email);
}
}
?>
<!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>
<table>
<caption>学生信息表</caption>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>邮箱</th>
</tr>
</thead>
<tbody>
<?php foreach ($staffs as $staff) : extract($staff) ?>
<tr>
<td><?php echo $id ?></td>
<td><?php echo $name ?></td>
<td><?php echo $sex ?></td>
<td><?php echo $email ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<p>
<?php for($i = 1 ;$i <= $pages;$i++): ?>
<?php
//!$_SERVER:服务器和执行环境信息,
//!$_SERVER('PHP_SELF'):当前执行脚本的文件名
$url = $_SERVER['PHP_SELF'] . '?p='.$i;
$activa = $i == $_GET['p'] ? 'active' : null;
?>
<a href="<?php echo $url ?>" class="<?php echo $activa ?>"><?php echo $i ?></a>
<?php endfor ?>
</p>
</body>