1.实例演示单文件上传
<!DOCTYPE html>
<html lang="zh-CN">
<?php
// $_files:PHP超全局变量,保存被上传文件的全部信息
printf('<pre>%s</pre>',print_r($_FILES,true));
/**
* 1.$_FILES:二维数组,每个元素对应一个上传的文件
* 2.name:原始文件名
* 3.type:文件类型,mime类型
* 4.tmp_name:临时目录
* 5.error:错误代码
* 6.size:文件大小,字节表示byte
*/
if(isset($_FILES['pic'])){
$name = $_FILES['pic']['name'];
$tmpName = $_FILES['pic']['tmp_name'];
$error = $_FILES['pic']['error'];
if($error > 0){
$tips = '<span style="color:red">上传失败:</span>';
switch($error){
case 1:
$tips .= '文件超过了php配置Uupload_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 "<h2>$tips</h2>";
}else{
//success
//判断用户是不是通过合法的POST方式上传
if(is_uploaded_file($tmpName)){
//设置允许上传文件类型的白名单
$allow = ['jpg','png'];
//获取文件扩展名
//pathinfo:分割文件名,文件后缀
// printf('<pre>%s</pre>',print_r(pathinfo($name),true));
$ext = pathinfo($name)['extension'];
//in_array()判断某个元素是否存在在一个数组中
if(in_array($ext,$allow)){
//两个条件都已满足
//1.POST方式上传
//2.文件类型是合法的
//设置目标目录
$path = 'uploads/';
//自定义目标文件名
$dest = $path . md5("$name") . '.' . $ext;
//将文件从临时目录移动到目标文件夹中
if(move_uploaded_file($tmpName,$dest)){
echo '上传成功';
// 预览
echo "<img src='$dest' width = '500'>";
}else {
echo '移动失败';
}
} else{
echo '文件类型不允许上传';
}
}else{
echo '文件上传方式不合法';
}
}
}
?>
<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>文件上传的表单构造,超全局变量$_FILE</title>
</head>
<body>
<!-- 允许上传文件的表单特征:
1.method:POST
2.enctype="multipart/form-data,二进制上传 -->
<form action="" method="POST" enctype="multipart/form-data" >
<fieldset>
<legend>单文件上传</legend>
<input type="hidden" name="MAX_FILE_SIZE" value="3000000000">
<input type="file" name="pic" id="">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
2.实例演示多文件上传
1.逐个上传
<!DOCTYPE html>
<html lang="zh-CN">
<?php
// $_files:PHP超全局变量,保存被上传文件的全部信息
printf('<pre>%s</pre>',print_r($_FILES,true));
foreach($_FILES as $file){
//$file中保存着每一个文件的信息
printf('<pre>%s</pre>',print_r($file,true));
if($file['error']===0){
$ext = pathinfo($file['name'])['extension'];
$destFile = 'uploads/'. md5($file['name']) . '.' . $ext;
move_uploaded_file($file['tmp_name'],$destFile);
echo "<img src='$destFile' width = '500'>";
}
}
?>
<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="pic1" id="">
<input type="file" name="pic2" id="">
<input type="file" name="pic3" id="">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
2.数组上传接收
<!DOCTYPE html>
<html lang="zh-CN">
<?php
// $_files:PHP超全局变量,保存被上传文件的全部信息
printf('<pre>%s</pre>',print_r($_FILES,true));
if(isset($_FILES['pic'])){
//$_FILES是一个三维数组,判断[error]===0即可
foreach($_FILES['pic']['error'] as $key=>$error){
if($error ===0){
//临时文件名
$tmpName = $_FILES['pic']['tmp_name'][$key];
//原始文件名
$name = $_FILES['pic']['name'][$key];
//目标文件名
$ext = pathinfo($name)['extension'];
$destFile = 'uploads/'. md5($name) . '.' . $ext;
//上传
move_uploaded_file($tmpName,$destFile);
echo "<img src='$destFile' width = '500'>";
}
}
}
?>
<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>多文件上传:逐个上传</legend>
<input type="file" name="pic[]" id="">
<input type="file" name="pic[]" id="">
<input type="file" name="pic[]" id="">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
3.多文件上传:批量上传
<!DOCTYPE html>
<html lang="zh-CN">
<?php
// $_files:PHP超全局变量,保存被上传文件的全部信息
printf('<pre>%s</pre>',print_r($_FILES,true));
if(isset($_FILES['pic'])){
//$_FILES是一个三维数组,判断[error]===0即可
foreach($_FILES['pic']['error'] as $key=>$error){
if($error ===0){
//临时文件名
$tmpName = $_FILES['pic']['tmp_name'][$key];
//原始文件名
$name = $_FILES['pic']['name'][$key];
//目标文件名
$ext = pathinfo($name)['extension'];
$destFile = 'uploads/'. md5($name) . '.' . $ext;
//上传
move_uploaded_file($tmpName,$destFile);
echo "<img src='$destFile' width = '500'>";
}
}
}
?>
<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>
<!-- multiple:允许同时选择多个上传 -->
<input type="file" name="pic[]" multiple>
<button>上传</button>
</fieldset>
</form>
</body>
</html>
3.分页
分页逻辑
<?php
// 1. 连接数据库
$db = new PDO('mysql:dbname=phpedu', 'root', 'root');
// 2. 当前页, 在GET参数中
// https://www.php.cn/course.html?p=5
// $page = isset($_GET['p']) ? $_GET['p'] : 1;
// null合并
$page = $_GET['p'] ?? 1;
echo "当前页: p= $page <br>";
// 3. 每页显示数量
$num = 5;
// 4. 记录总数
$sql = 'SELECT COUNT(`id`) AS `total` FROM `staff`';
$stmt = $db->prepare($sql);
$stmt->execute();
// 将某列的仠与php变量绑定 , `total` => $total
$stmt->bindColumn('total', $total);
$stmt->fetch(PDO::FETCH_ASSOC);
echo "总记录数量: $total <br>";
// 5. 总页数
// 10.1 => 11 ceil: 向上取整,不丢数据
$pages = ceil($total / $num);
echo "总页数: $pages <br>";
// 6. 偏移量
// offset = (page - 1) * num
$offset = ($page - 1) * $num;
echo "偏移量: $offset <br>";
// 7. 分页数据
// $sql = "SELECT * FROM `staff` LIMIT $num OFFSET $offset";
$sql = "SELECT * FROM `staff` LIMIT $offset, $num";
$stmt = $db->prepare($sql);
$stmt->execute();
$staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
// 遍历
echo '<hr>';
if (count($staffs) === 0) {
echo '查询结果为空';
} else {
foreach ($staffs as $staff) {
extract($staff);
printf('$d-%s-%s-%s<br>', $id, $name, $sex, $email);
}
}
echo '<hr>';
页面渲染分页数据
<?php require 'demo5.php' ?>
<!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>
<style>
table {
width: 400px;
border-collapse: collapse;
text-align: center;
}
table th,
table td {
border: 1px solid;
padding: 5px;
}
table thead {
background-color: lightcyan;
}
table caption {
font-size: larger;
margin-bottom: 8px;
}
p>a {
text-decoration: none;
color: #555;
border: 1px solid;
padding: 5px 10px;
margin: 10px 2px;
}
.active {
background-color: seagreen;
color: white;
border: 1px solid seagreen;
}
</style>
</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><?= $id ?></td>
<td><?= $name ?></td>
<td><?= $sex ?></td>
<td><?= $email ?></td>
</tr>
<?php endforeach ?>
</tbody>
</table>
<p>
<?php for ($i = 1; $i <= $pages; $i++) : ?>
<!-- <a href="<?= $_SERVER['PHP_SELF'] . '?p=' . $i ?>" class="<?= ($i == $_GET['p']) ? 'active' : null ?>"><?= $i ?></a> -->
<?php
$url = $_SERVER['PHP_SELF'] . '?p=' . $i;
$active = $i == $_GET['p'] ? 'active' : null;
?>
<a href="<?= $url ?>" class="<?= $active ?>"><?= $i ?></a>
<?php endfor ?>
</p>
</body>
</html>