博客列表 >实例演示单文件上传与分页原理及实现

实例演示单文件上传与分页原理及实现

李玉峰
李玉峰原创
2022年05月05日 17:45:20498浏览

1.实例演示单文件上传

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <?php
  4. // $_files:PHP超全局变量,保存被上传文件的全部信息
  5. printf('<pre>%s</pre>',print_r($_FILES,true));
  6. /**
  7. * 1.$_FILES:二维数组,每个元素对应一个上传的文件
  8. * 2.name:原始文件名
  9. * 3.type:文件类型,mime类型
  10. * 4.tmp_name:临时目录
  11. * 5.error:错误代码
  12. * 6.size:文件大小,字节表示byte
  13. */
  14. if(isset($_FILES['pic'])){
  15. $name = $_FILES['pic']['name'];
  16. $tmpName = $_FILES['pic']['tmp_name'];
  17. $error = $_FILES['pic']['error'];
  18. if($error > 0){
  19. $tips = '<span style="color:red">上传失败:</span>';
  20. switch($error){
  21. case 1:
  22. $tips .= '文件超过了php配置Uupload_max_filesize限制的值';
  23. break;
  24. case 2:
  25. $tips .= '文件大小超过了MAX_FILE_SIZE最大值';
  26. break;
  27. case 3:
  28. $tips .= '文件只有部分被上传';
  29. break;
  30. case 4:
  31. $tips .= '没有文件被上传';
  32. break;
  33. case 6:
  34. $tips .= '找不到临时文件夹';
  35. break;
  36. case 7:
  37. $tips .= '文件写入失败,请检查目录权限';
  38. break;
  39. }
  40. echo "<h2>$tips</h2>";
  41. }else{
  42. //success
  43. //判断用户是不是通过合法的POST方式上传
  44. if(is_uploaded_file($tmpName)){
  45. //设置允许上传文件类型的白名单
  46. $allow = ['jpg','png'];
  47. //获取文件扩展名
  48. //pathinfo:分割文件名,文件后缀
  49. // printf('<pre>%s</pre>',print_r(pathinfo($name),true));
  50. $ext = pathinfo($name)['extension'];
  51. //in_array()判断某个元素是否存在在一个数组中
  52. if(in_array($ext,$allow)){
  53. //两个条件都已满足
  54. //1.POST方式上传
  55. //2.文件类型是合法的
  56. //设置目标目录
  57. $path = 'uploads/';
  58. //自定义目标文件名
  59. $dest = $path . md5("$name") . '.' . $ext;
  60. //将文件从临时目录移动到目标文件夹中
  61. if(move_uploaded_file($tmpName,$dest)){
  62. echo '上传成功';
  63. // 预览
  64. echo "<img src='$dest' width = '500'>";
  65. }else {
  66. echo '移动失败';
  67. }
  68. } else{
  69. echo '文件类型不允许上传';
  70. }
  71. }else{
  72. echo '文件上传方式不合法';
  73. }
  74. }
  75. }
  76. ?>
  77. <head>
  78. <meta charset="UTF-8">
  79. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  80. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  81. <title>文件上传的表单构造,超全局变量$_FILE</title>
  82. </head>
  83. <body>
  84. <!-- 允许上传文件的表单特征:
  85. 1.method:POST
  86. 2.enctype="multipart/form-data,二进制上传 -->
  87. <form action="" method="POST" enctype="multipart/form-data" >
  88. <fieldset>
  89. <legend>单文件上传</legend>
  90. <input type="hidden" name="MAX_FILE_SIZE" value="3000000000">
  91. <input type="file" name="pic" id="">
  92. <button>上传</button>
  93. </fieldset>
  94. </form>
  95. </body>
  96. </html>

2.实例演示多文件上传

1.逐个上传

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <?php
  4. // $_files:PHP超全局变量,保存被上传文件的全部信息
  5. printf('<pre>%s</pre>',print_r($_FILES,true));
  6. foreach($_FILES as $file){
  7. //$file中保存着每一个文件的信息
  8. printf('<pre>%s</pre>',print_r($file,true));
  9. if($file['error']===0){
  10. $ext = pathinfo($file['name'])['extension'];
  11. $destFile = 'uploads/'. md5($file['name']) . '.' . $ext;
  12. move_uploaded_file($file['tmp_name'],$destFile);
  13. echo "<img src='$destFile' width = '500'>";
  14. }
  15. }
  16. ?>
  17. <head>
  18. <meta charset="UTF-8">
  19. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  20. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  21. <title>多文件上传:逐个上传</title>
  22. </head>
  23. <body>
  24. <form action="" method="POST" enctype="multipart/form-data" >
  25. <fieldset>
  26. <legend>多文件上传:逐个上传</legend>
  27. <input type="file" name="pic1" id="">
  28. <input type="file" name="pic2" id="">
  29. <input type="file" name="pic3" id="">
  30. <button>上传</button>
  31. </fieldset>
  32. </form>
  33. </body>
  34. </html>

2.数组上传接收

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <?php
  4. // $_files:PHP超全局变量,保存被上传文件的全部信息
  5. printf('<pre>%s</pre>',print_r($_FILES,true));
  6. if(isset($_FILES['pic'])){
  7. //$_FILES是一个三维数组,判断[error]===0即可
  8. foreach($_FILES['pic']['error'] as $key=>$error){
  9. if($error ===0){
  10. //临时文件名
  11. $tmpName = $_FILES['pic']['tmp_name'][$key];
  12. //原始文件名
  13. $name = $_FILES['pic']['name'][$key];
  14. //目标文件名
  15. $ext = pathinfo($name)['extension'];
  16. $destFile = 'uploads/'. md5($name) . '.' . $ext;
  17. //上传
  18. move_uploaded_file($tmpName,$destFile);
  19. echo "<img src='$destFile' width = '500'>";
  20. }
  21. }
  22. }
  23. ?>
  24. <head>
  25. <meta charset="UTF-8">
  26. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  27. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  28. <title>多文件上传:逐个上传2</title>
  29. </head>
  30. <body>
  31. <form action="" method="POST" enctype="multipart/form-data" >
  32. <fieldset>
  33. <legend>多文件上传:逐个上传</legend>
  34. <input type="file" name="pic[]" id="">
  35. <input type="file" name="pic[]" id="">
  36. <input type="file" name="pic[]" id="">
  37. <button>上传</button>
  38. </fieldset>
  39. </form>
  40. </body>
  41. </html>

3.多文件上传:批量上传

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <?php
  4. // $_files:PHP超全局变量,保存被上传文件的全部信息
  5. printf('<pre>%s</pre>',print_r($_FILES,true));
  6. if(isset($_FILES['pic'])){
  7. //$_FILES是一个三维数组,判断[error]===0即可
  8. foreach($_FILES['pic']['error'] as $key=>$error){
  9. if($error ===0){
  10. //临时文件名
  11. $tmpName = $_FILES['pic']['tmp_name'][$key];
  12. //原始文件名
  13. $name = $_FILES['pic']['name'][$key];
  14. //目标文件名
  15. $ext = pathinfo($name)['extension'];
  16. $destFile = 'uploads/'. md5($name) . '.' . $ext;
  17. //上传
  18. move_uploaded_file($tmpName,$destFile);
  19. echo "<img src='$destFile' width = '500'>";
  20. }
  21. }
  22. }
  23. ?>
  24. <head>
  25. <meta charset="UTF-8">
  26. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  27. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  28. <title>多文件上传:批量上传</title>
  29. </head>
  30. <body>
  31. <form action="" method="POST" enctype="multipart/form-data" >
  32. <fieldset>
  33. <legend>多文件上传:批量上传</legend>
  34. <!-- multiple:允许同时选择多个上传 -->
  35. <input type="file" name="pic[]" multiple>
  36. <button>上传</button>
  37. </fieldset>
  38. </form>
  39. </body>
  40. </html>

3.分页

分页逻辑

  1. <?php
  2. // 1. 连接数据库
  3. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  4. // 2. 当前页, 在GET参数中
  5. // https://www.php.cn/course.html?p=5
  6. // $page = isset($_GET['p']) ? $_GET['p'] : 1;
  7. // null合并
  8. $page = $_GET['p'] ?? 1;
  9. echo "当前页: p= $page <br>";
  10. // 3. 每页显示数量
  11. $num = 5;
  12. // 4. 记录总数
  13. $sql = 'SELECT COUNT(`id`) AS `total` FROM `staff`';
  14. $stmt = $db->prepare($sql);
  15. $stmt->execute();
  16. // 将某列的仠与php变量绑定 , `total` => $total
  17. $stmt->bindColumn('total', $total);
  18. $stmt->fetch(PDO::FETCH_ASSOC);
  19. echo "总记录数量: $total <br>";
  20. // 5. 总页数
  21. // 10.1 => 11 ceil: 向上取整,不丢数据
  22. $pages = ceil($total / $num);
  23. echo "总页数: $pages <br>";
  24. // 6. 偏移量
  25. // offset = (page - 1) * num
  26. $offset = ($page - 1) * $num;
  27. echo "偏移量: $offset <br>";
  28. // 7. 分页数据
  29. // $sql = "SELECT * FROM `staff` LIMIT $num OFFSET $offset";
  30. $sql = "SELECT * FROM `staff` LIMIT $offset, $num";
  31. $stmt = $db->prepare($sql);
  32. $stmt->execute();
  33. $staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  34. // 遍历
  35. echo '<hr>';
  36. if (count($staffs) === 0) {
  37. echo '查询结果为空';
  38. } else {
  39. foreach ($staffs as $staff) {
  40. extract($staff);
  41. printf('$d-%s-%s-%s<br>', $id, $name, $sex, $email);
  42. }
  43. }
  44. echo '<hr>';

页面渲染分页数据

  1. <?php require 'demo5.php' ?>
  2. <!DOCTYPE html>
  3. <html lang="zh-CN">
  4. <head>
  5. <meta charset="UTF-8">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  8. <title>页面展示分页数据</title>
  9. <style>
  10. table {
  11. width: 400px;
  12. border-collapse: collapse;
  13. text-align: center;
  14. }
  15. table th,
  16. table td {
  17. border: 1px solid;
  18. padding: 5px;
  19. }
  20. table thead {
  21. background-color: lightcyan;
  22. }
  23. table caption {
  24. font-size: larger;
  25. margin-bottom: 8px;
  26. }
  27. p>a {
  28. text-decoration: none;
  29. color: #555;
  30. border: 1px solid;
  31. padding: 5px 10px;
  32. margin: 10px 2px;
  33. }
  34. .active {
  35. background-color: seagreen;
  36. color: white;
  37. border: 1px solid seagreen;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <table>
  43. <caption>员工信息表</caption>
  44. <thead>
  45. <tr>
  46. <th>ID</th>
  47. <th>姓名</th>
  48. <th>性别</th>
  49. <th>邮箱</th>
  50. </tr>
  51. </thead>
  52. <tbody>
  53. <?php foreach ($staffs as $staff) : extract($staff) ?>
  54. <tr>
  55. <td><?= $id ?></td>
  56. <td><?= $name ?></td>
  57. <td><?= $sex ?></td>
  58. <td><?= $email ?></td>
  59. </tr>
  60. <?php endforeach ?>
  61. </tbody>
  62. </table>
  63. <p>
  64. <?php for ($i = 1; $i <= $pages; $i++) : ?>
  65. <!-- <a href="<?= $_SERVER['PHP_SELF'] . '?p=' . $i ?>" class="<?= ($i == $_GET['p']) ? 'active' : null ?>"><?= $i ?></a> -->
  66. <?php
  67. $url = $_SERVER['PHP_SELF'] . '?p=' . $i;
  68. $active = $i == $_GET['p'] ? 'active' : null;
  69. ?>
  70. <a href="<?= $url ?>" class="<?= $active ?>"><?= $i ?></a>
  71. <?php endfor ?>
  72. </p>
  73. </body>
  74. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议