博客列表 >演示单文件上传与多文件上传、演示简单分页操作,并理解分页所需参数的意思

演示单文件上传与多文件上传、演示简单分页操作,并理解分页所需参数的意思

Blackeye
Blackeye原创
2022年04月29日 10:24:10443浏览

1

  1. <?php
  2. // printf('<pre>%s</pre>',print_r($_FILES,true));
  3. // 1. 实例演示单文件上传与多文件上传
  4. // 单文件
  5. if(isset($_FILES['myfile'])){
  6. extract($_FILES['myfile']);
  7. if($error===0){
  8. $ext = pathinfo($name)['extension'];
  9. $dest = 'uploads/'. md5($name) . '.' . $ext;
  10. if(move_uploaded_file($tmp_name,$dest)){
  11. echo '<span style="color:green">上传成功</span><br>';
  12. echo "<img src=$dest width='200'>";
  13. }
  14. }
  15. }
  16. ?>
  17. <!DOCTYPE html>
  18. <html lang="en">
  19. <head>
  20. <meta charset="UTF-8">
  21. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  22. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  23. <title>单文件上传</title>
  24. </head>
  25. <body>
  26. <form action="" method="post" enctype="multipart/form-data">
  27. <fieldset>
  28. <legend>单文件上传</legend>
  29. <input type="file" name="myfile">
  30. <button>上传</button>
  31. </fieldset>
  32. </form>
  33. </body>
  34. </html>

2

  1. <?php
  2. //printf('<pre>%s</pre>',print_r($_FILES,true));
  3. // 多文件上传
  4. if(isset($_FILES['myfile'])){
  5. foreach($_FILES['myfile']['error'] as $key=>$error){
  6. if($error===0){
  7. $name = $_FILES['myfile']['name'][$key];
  8. $tmpName = $_FILES['myfile']['tmp_name'][$key];
  9. $desc = 'uploads/'. md5($name).'.'. pathinfo($name)['extension'];
  10. move_uploaded_file($tmpName,$desc);
  11. echo '<span style="color:green">上传成功</span><br>';
  12. echo "<img src=$desc width='200'><br>";
  13. }
  14. }
  15. }
  16. ?>
  17. <!DOCTYPE html>
  18. <html lang="en">
  19. <head>
  20. <meta charset="UTF-8">
  21. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  22. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  23. <title>多文件上传</title>
  24. </head>
  25. <body>
  26. <form action="" method="post" enctype="multipart/form-data">
  27. <fieldset>
  28. <legend>多文件上传</legend>
  29. <input type="file" name="myfile[]" multiple>
  30. <button>上传</button>
  31. </fieldset>
  32. </form>
  33. </body>
  34. </html>

3

  1. <?php
  2. $p = $_GET['p']??1;
  3. // 2. 实例演示简单分页操作,并理解分页所需参数的意思
  4. $db = new PDO('mysql:dbname=phpedu','root','root');
  5. // 数据表总数量:Total
  6. $sql = 'SELECT COUNT(*) AS `total` FROM `staff`';
  7. $stmt = $db->prepare($sql);
  8. $stmt->execute();
  9. $stmt->bindColumn('total', $total);
  10. $stmt->fetch(PDO::FETCH_ASSOC);
  11. // 单页显示数量:num=5
  12. $num = 5;
  13. // 分页数量
  14. $pages = ceil($total/$num);
  15. // 计算偏移量
  16. $offset = ($p-1)*$num;
  17. $sql = "SELECT * FROM `staff` LIMIT $num OFFSET $offset";
  18. $stmt = $db->prepare($sql);
  19. $stmt->execute();
  20. $staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  21. ?>
  22. <!DOCTYPE html>
  23. <html lang="en">
  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. <style>
  30. table {
  31. width: 400px;
  32. border-collapse: collapse;
  33. text-align: center;
  34. }
  35. table th,
  36. table td {
  37. border: 1px solid;
  38. padding: 5px;
  39. }
  40. table thead {
  41. background-color: lightcyan;
  42. }
  43. table caption {
  44. font-size: larger;
  45. margin-bottom: 8px;
  46. }
  47. p>a {
  48. text-decoration: none;
  49. color: #555;
  50. border: 1px solid;
  51. padding: 5px 10px;
  52. margin: 10px 2px;
  53. }
  54. .active {
  55. background-color: seagreen;
  56. color: white;
  57. border: 1px solid seagreen;
  58. }
  59. </style>
  60. </head>
  61. <body>
  62. <table>
  63. <caption>员工信息表</caption>
  64. <thead>
  65. <tr>
  66. <th>ID</th>
  67. <th>姓名</th>
  68. <th>性别</th>
  69. <th>邮箱</th>
  70. </tr>
  71. </thead>
  72. <tbody>
  73. <?php foreach ($staffs as $staff) : extract($staff) ?>
  74. <tr>
  75. <td><?= $id ?></td>
  76. <td><?= $name ?></td>
  77. <td><?= $gender ?></td>
  78. <td><?= $email ?></td>
  79. </tr>
  80. <?php endforeach ?>
  81. </tbody>
  82. </table>
  83. <p>
  84. <?php for ($i = 1; $i <= $pages; $i++) : ?>
  85. <?php
  86. $url = $_SERVER['PHP_SELF'] . '?p=' . $i;
  87. $active = $i == $_GET['p'] ? 'active' : null;
  88. ?>
  89. <a href="<?= $url ?>" class="<?= $active ?>"><?= $i ?></a>
  90. <?php endfor ?>
  91. </p>
  92. </body>
  93. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议