Blogger Information
Blog 33
fans 0
comment 0
visits 17090
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
单文件上传与多文件上传,简单分页操作的实例演示
lucaslwk
Original
533 people have browsed it

单文件上传与多文件上传,简单分页操作的实例演示

单文件上传与多文件文件上传-逐个上传

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. printf('<pre>%s</pre>', print_r($_FILES, true));
  5. foreach ($_FILES as $file) {
  6. extract($file);
  7. if ($error > 0) {
  8. $tips = '<span style="color:red">上传失败:</span>';
  9. switch ($error) {
  10. case '1':
  11. $tips .= '文件超过系统设置的最大大小';
  12. break;
  13. case '2':
  14. $tips .= '文件超过表单设置的最大大小';
  15. break;
  16. case '3':
  17. $tips .= '文件部分被上传';
  18. break;
  19. case '4':
  20. $tips .= '无文件上传';
  21. break;
  22. case '6':
  23. $tips .= '找不到临时目录';
  24. break;
  25. case '7':
  26. $tips .= '文件写入失败';
  27. break;
  28. }
  29. echo $tips;
  30. } else {
  31. //判断指定的文件是否是通过 HTTP POST 上传的
  32. if (is_uploaded_file($tmp_name)) {
  33. //设置允许上传的文件类型
  34. $allow = ['jpg', 'jpeg', 'png', 'bmp'];
  35. //获取文件的扩展名
  36. $ext = pathinfo($name)['extension'];
  37. //判断文件类型是够合法
  38. if (in_array($ext, $allow)) {
  39. //目标路径
  40. $path = 'upload/';
  41. //自定义目标文件名
  42. $dest = $path . md5($name) . '.' . $ext;
  43. //将文件从临时目录中移动到目标目录中并重命名
  44. if (move_uploaded_file($tmp_name, $dest)) {
  45. echo '<span style="color:red">上传成功<br></span>';
  46. //预览
  47. echo "<img src='$dest' width='200px'></img><br>";
  48. } else echo '<span style="color:red">移动失败</span>';
  49. } else echo '<span style="color:red">非法文件类型</span>';
  50. } else echo '<span style="color:red">非法方式上传</span>';
  51. }
  52. }
  53. ?>
  54. <head>
  55. <meta charset="UTF-8">
  56. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  57. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  58. <title>单文件上传</title>
  59. </head>
  60. <body>
  61. <!-- 允许上传文件的表单:
  62. 1.method = "POST";
  63. 2.enctype = "multipart/form-data" 规定在提交表单时要需要的内容类型
  64. -->
  65. <form action="" method="POST" enctype="multipart/form-data">
  66. <fieldset>
  67. <legend>单文件上传</legend>
  68. <input type="file" name="my_pic">
  69. <button>上传</button>
  70. </fieldset>
  71. </form>
  72. <form action="" method="POST" enctype="multipart/form-data">
  73. <fieldset>
  74. <legend>多文件文件上传-逐个上传</legend>
  75. <input type="file" name="my_pic">
  76. <input type="file" name="my_pic1">
  77. <input type="file" name="my_pic2">
  78. <button>上传</button>
  79. </fieldset>
  80. </form>
  81. </body>
  82. </html>

多文件文件上传-逐个上传 v2 与多文件文件上传-多个上传

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. printf('<pre>%s</pre>', print_r($_FILES, true));
  5. if (isset($_FILES['my_pic'])) {
  6. //遍历$_FILES['my_pic']['error']数组,值>0
  7. $errorArray = $_FILES['my_pic']['error'];
  8. foreach ($errorArray as $key => $error) {
  9. $tmp_name = $_FILES['my_pic']['tmp_name'][$key];
  10. $name = $_FILES['my_pic']['name'][$key];
  11. if ($error > 0) {
  12. $tips = '<span style="color:red">上传失败:</span>';
  13. switch ($error) {
  14. case '1':
  15. $tips .= '文件超过系统设置的最大大小';
  16. break;
  17. case '2':
  18. $tips .= '文件超过表单设置的最大大小';
  19. break;
  20. case '3':
  21. $tips .= '文件部分被上传';
  22. break;
  23. case '4':
  24. $tips .= '无文件上传';
  25. break;
  26. case '6':
  27. $tips .= '找不到临时目录';
  28. break;
  29. case '7':
  30. $tips .= '文件写入失败';
  31. break;
  32. }
  33. echo $tips;
  34. } else {
  35. //判断指定的文件是否是通过 HTTP POST 上传的
  36. if (is_uploaded_file($tmp_name)) {
  37. //设置允许上传的文件类型
  38. $allow = ['jpg', 'jpeg', 'png', 'bmp'];
  39. //获取文件的扩展名
  40. $ext = pathinfo($name)['extension'];
  41. //判断文件类型是够合法
  42. if (in_array($ext, $allow)) {
  43. //目标路径
  44. $path = 'upload/';
  45. //自定义目标文件名
  46. $dest = $path . md5($name) . '.' . $ext;
  47. //将文件从临时目录中移动到目标目录中并重命名
  48. if (move_uploaded_file($tmp_name, $dest)) {
  49. echo '<span style="color:red">上传成功<br></span>';
  50. //预览
  51. echo "<img src='$dest' width='200px'></img><br>";
  52. } else echo '<span style="color:red">移动失败</span>';
  53. } else echo '<span style="color:red">非法文件类型</span>';
  54. } else echo '<span style="color:red">非法方式上传</span>';
  55. }
  56. };
  57. } else echo '<span style="color:red">上传失败</span>';
  58. ?>
  59. <head>
  60. <meta charset="UTF-8">
  61. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  62. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  63. <title>多文件上传</title>
  64. </head>
  65. <body>
  66. <!-- 允许上传文件的表单:
  67. 1.method = "POST";
  68. 2.enctype = "multipart/form-data" 规定在提交表单时要需要的内容类型
  69. -->
  70. <form action="" method="POST" enctype="multipart/form-data">
  71. <fieldset>
  72. <legend>多文件文件上传-逐个上传v2</legend>
  73. <!-- 使用数组表示每个要上传的表单域的名称 -->
  74. <input type="file" name="my_pic[]">
  75. <input type="file" name="my_pic[]">
  76. <input type="file" name="my_pic[]">
  77. <button>上传</button>
  78. </fieldset>
  79. </form>
  80. <form action="" method="POST" enctype="multipart/form-data">
  81. <fieldset>
  82. <legend>多文件文件上传-多个上传</legend>
  83. <!-- multiple:允许同时选择多个 -->
  84. <input type="file" name="my_pic[]" multiple>
  85. <button>上传</button>
  86. </fieldset>
  87. </form>
  88. </body>
  89. </html>

简单分页操作的实例演示

  1. <?php
  2. //连接数据库
  3. $db = new PDO('mysql:dbname=phpedu', 'root', 'root');
  4. //当前页
  5. $page = $_GET['p'] ?? 1;
  6. echo '当前页:' . $page . '<br>';
  7. //每页显示数量
  8. $num = 5;
  9. //记录总数
  10. $sql = 'SELECT COUNT(`id`) AS `total` FROM `staff`';
  11. $stmt = $db->prepare($sql);
  12. $stmt->execute();
  13. $stmt->bindColumn('total', $total);
  14. $stmt->fetch(PDO::FETCH_ASSOC);
  15. echo '记录总数:' . $total . '<br>';
  16. //总页数
  17. $pages = ceil($total / $num);
  18. echo '总页数:' . $pages . '<br>';
  19. //每页的起始索引,偏移量
  20. $offset = ($page - 1) * $num;
  21. echo '偏移量:' . $offset . '<hr>';
  22. //分页数据遍历
  23. $sql = "SELECT * FROM `staff` LIMIT $num OFFSET $offset";
  24. $stmt = $db->prepare($sql);
  25. $stmt->execute();
  26. $staffs = $stmt->fetchAll(PDO::FETCH_ASSOC);
  27. if (count($staffs) === 0) {
  28. echo '查询结果为空';
  29. } else {
  30. foreach ($staffs as $staff) {
  31. extract($staff);
  32. echo "$id-$name-$gender-$email<br>";
  33. }
  34. }
  35. echo '<hr>';
  36. ?>
  37. <!DOCTYPE html>
  38. <html lang="en">
  39. <head>
  40. <meta charset="UTF-8">
  41. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  42. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  43. <title>分页显示</title>
  44. </head>
  45. <style>
  46. table {
  47. width: 400px;
  48. text-align: center;
  49. border-collapse: collapse;
  50. }
  51. table th,
  52. table td {
  53. border: 1px solid black;
  54. padding: 5px;
  55. }
  56. table caption {
  57. font-size: larger;
  58. margin-bottom: 8px;
  59. }
  60. table thead {
  61. background-color: lightcyan;
  62. }
  63. p>a {
  64. text-decoration: none;
  65. color: black;
  66. border: 1px solid black;
  67. padding: 5px 10px;
  68. margin: 10px 2px;
  69. }
  70. .active {
  71. color: white;
  72. background-color: seagreen;
  73. border: 1px solid white;
  74. }
  75. </style>
  76. <body>
  77. <table>
  78. <caption>员工信息表</caption>
  79. <thead>
  80. <tr>
  81. <th>序号</th>
  82. <th>姓名</th>
  83. <th>性别</th>
  84. <th>邮箱</th>
  85. </tr>
  86. </thead>
  87. <tbody>
  88. <?php foreach ($staffs as $staff) : extract($staff) ?>
  89. <tr>
  90. <td><?= $id ?></td>
  91. <td><?= $name ?></td>
  92. <td><?= ($gender === '1') ? '男' : '女' ?></td>
  93. <td><?= $email ?></td>
  94. </tr>
  95. <?php endforeach ?>
  96. </tbody>
  97. </table>
  98. <p>
  99. <?php for ($i = 1; $i <= $pages; $i++) : ?>
  100. <?php
  101. //$_SERVER['PHP_SELF']返回当前执行脚本的文件名
  102. $url = $_SERVER['PHP_SELF'] . '?p=' . $i;
  103. //$i int类型;$page string类型,不能用===
  104. $active = ($i == $page) ? 'active' : null;
  105. ?>
  106. <a href="<?= $url ?>" class="<?= $active ?>"><?= $i ?></a>
  107. <?php endfor ?>
  108. </p>
  109. </body>
  110. </html>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post