Blogger Information
Blog 47
fans 1
comment 0
visits 40570
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
单文件与多文件上传,分页操作
新手1314
Original
822 people have browsed it

单文件上传

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. //! $_FILES: PHP超全局变量数量, 保存着上传文件的全部信息
  5. printf('<pre>%s</pre>', print_r($_FILES,true));
  6. //!isset: 检测变量是否已声明并且其值不为 null
  7. if(isset($_FILES['my_pic'])){
  8. $name = $_FILES['my_pic']['name'];
  9. $tmpName = $_FILES['my_pic']['tmp_name'];
  10. $error = $_FILES['my_pic']['error'];
  11. if($error>0){
  12. $tips = '<span style="color:lightgreen">上传失败:</span>';
  13. switch($error){
  14. case 1 :
  15. $tips .='上传的文件超过了php.ini 中upload_max_filesize 选项限制的值';
  16. break;
  17. case 2 :
  18. $tips .='文件大小超过了上传表单中MAX_FILE_SIZE最大值';
  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 "<p>$tips</p>";
  34. }else{
  35. //! is_uploaded_file:判断文件是否是通过 HTTP POST 上传的
  36. if(is_uploaded_file($tmpName)){
  37. $allow = ['jpg','jpeg','png','gif'];
  38. //! pathinfo:返回文件路径的信息,
  39. //!如果没有传入 options ,将会返回包括以下单元的数组 array:dirname,basename 和 extension
  40. $ext = pathinfo($name)['extension'];
  41. // print_r($ext);
  42. //! in_array:检查数组中是否存在某个值
  43. if(in_array($ext,$allow)){
  44. // $path = 'C:\Users\Administrator\Desktop\后端学习质料\0427-1\up/';
  45. $path = 'C:/Users/Administrator/Desktop/后端学习质料/0427/up/';
  46. //!将目标文件名md5加密
  47. $dest =$path . md5($name) . '.' . $ext;
  48. //预览文件存放的地方
  49. $show = 'up/'.md5($name). '.' . $ext;
  50. // print_r($dest);
  51. //!move_uploaded_file: 将上传的文件移动到新位置
  52. if(move_uploaded_file($tmpName, $dest)){
  53. // move_uploaded_file($tmpName, $path);
  54. echo '<p style="color:red">上传成功</p>';
  55. echo "<img src='$show' width='200'>";
  56. }else{
  57. echo '<p style="color:lightblue">移动失败</p>';
  58. }
  59. }else{
  60. echo '<p style="color:lightblue">文件类型错误</p>';
  61. }
  62. }else{
  63. echo '<p style="color:lightblue">非法方式上传</p>';
  64. }
  65. }
  66. }
  67. ?>
  68. <head>
  69. <meta charset="UTF-8">
  70. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  71. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  72. <title>单文件上传</title>
  73. </head>
  74. <body>
  75. <!-- enctype:规定了form表单在发送到服务器时候,数据的编码方式 -->
  76. <!-- multipart/form-data: 指定传输数据为二进制类型,比如图片、mp3、文件。 -->
  77. <form action="" method="POST" enctype="multipart/form-data">
  78. <fieldset>
  79. <legend>单文件上传</legend>
  80. <!-- type="hidden":隐藏域 -->
  81. <!-- MAX_FILE_SIZE,意思是接收文件的最大尺寸,与后面的value的值对应 -->
  82. <input type="hidden" name="MAX_FILE_SIZE" value="300000">
  83. <input type="file" name="my_pic">
  84. <button>上传</button>
  85. </fieldset>
  86. </form>
  87. </body>
  88. </html>

多文件上传

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. printf('<pre>%s</pre>',print_r($_FILES,true));
  5. foreach($_FILES as $file){
  6. if($file['error']>0){
  7. $tips = '<span style="color:lightgreen">上传失败:</span>';
  8. switch($file['error']){
  9. case 1 :
  10. $tips .='上传的文件超过了php.ini 中upload_max_filesize 选项限制的值';
  11. break;
  12. case 2 :
  13. $tips .='文件大小超过了上传表单中MAX_FILE_SIZE最大值';
  14. break;
  15. case 3 :
  16. $tips .='文件只有部分被上传';
  17. break;
  18. case 4 :
  19. $tips .='没有文件被上传';
  20. break;
  21. case 6 :
  22. $tips .='找不到临时目录';
  23. break;
  24. case 7 :
  25. $tips .='文件写入失败,请检查目录权限';
  26. break;
  27. }
  28. echo "<p>$tips</p>";
  29. }else{
  30. if(is_uploaded_file($file['tmp_name'])){
  31. $allow = ['jpg','jpeg','png','gif'];
  32. //! pathinfo:返回文件路径的信息,
  33. //!如果没有传入 options ,将会返回包括以下单元的数组 array:dirname,basename 和 extension
  34. $ext = pathinfo($file['name'])['extension'];
  35. // print_r($ext);
  36. //! in_array:检查数组中是否存在某个值
  37. if(in_array($ext,$allow)){
  38. // $path = 'C:\Users\Administrator\Desktop\后端学习质料\0427-1\up/';
  39. $path = 'C:/Users/Administrator/Desktop/后端学习质料/0427/ups/';
  40. //!将目标文件名md5加密
  41. $dest =$path . md5($file['name']) . '.' . $ext;
  42. $show = 'ups/'.md5($file['name']). '.' . $ext;
  43. // $dest =$name;
  44. // print_r($dest);
  45. //!move_uploaded_file: 将上传的文件移动到新位置
  46. if(move_uploaded_file($file['tmp_name'], $dest)){
  47. // move_uploaded_file($tmpName, $path);
  48. echo '<p style="color:red">上传成功</p>';
  49. echo "<img src='$show' width='200'>";
  50. }else{
  51. echo '<p style="color:lightblue">移动失败</p>';
  52. }
  53. }else{
  54. echo '<p style="color:lightblue">文件类型错误</p>';
  55. }
  56. }else{
  57. echo '<p style="color:lightblue">非法方式上传</p>';
  58. }
  59. }
  60. }
  61. ?>
  62. <head>
  63. <meta charset="UTF-8">
  64. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  65. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  66. <title>多文件上传</title>
  67. </head>
  68. <body>
  69. <form action="" method="post" enctype="multipart/form-data">
  70. <fieldset>
  71. <legend>多文件上传</legend>
  72. <input type="file" name="my_pic1">
  73. <input type="file" name="my_pic2">
  74. <input type="file" name="my_pic3">
  75. <button>上传</button>
  76. </fieldset>
  77. </form>
  78. </body>
  79. </html>

分页操作

  1. <?php
  2. //1.连接数据库
  3. $db = new PDO('mysql:dbname=php','root','root');
  4. //2.获取当前页,设置默认值为1.
  5. $now= $_GET['p']?? 1;
  6. //3.每页显示数量
  7. $num = 5;
  8. //4.获取数据库数据总条数
  9. $sql = 'SELECT count(`id`) as `total` from `student`';
  10. $stmt = $db ->prepare($sql);
  11. $stmt -> execute();
  12. //!bindColumn:绑定一列到一个 PHP 变量(类似于list()函数为变量赋值)
  13. $stmt ->bindColumn('total',$total);
  14. $stmt -> fetch(PDO::FETCH_ASSOC);
  15. //5.总页数
  16. //! ceil:向上取整
  17. $pages= ceil($total / $num);
  18. //6.偏移量
  19. $off= ($now - 1) * $num;
  20. //7.分页数据
  21. //!查询分页
  22. $sql = "SELECT * from `student` limit $num offset $off";
  23. $stmt = $db ->prepare($sql);
  24. $stmt -> execute();
  25. //读取所有分页里的数据
  26. $staffs = $stmt ->fetchAll(PDO::FETCH_ASSOC);
  27. //遍历并输出
  28. if(count($staffs)===0){
  29. echo '查询结果为空';
  30. }else{
  31. foreach($staffs as staff){
  32. extract($staff);
  33. printf('<pre>%d,%s,%s,%s</pre',$id,$name,$sex,$email);
  34. }
  35. }
  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. <body>
  46. <table>
  47. <caption>学生信息表</caption>
  48. <thead>
  49. <tr>
  50. <th>ID</th>
  51. <th>姓名</th>
  52. <th>性别</th>
  53. <th>邮箱</th>
  54. </tr>
  55. </thead>
  56. <tbody>
  57. <?php foreach ($staffs as $staff) : extract($staff) ?>
  58. <tr>
  59. <td><?php echo $id ?></td>
  60. <td><?php echo $name ?></td>
  61. <td><?php echo $sex ?></td>
  62. <td><?php echo $email ?></td>
  63. </tr>
  64. <?php endforeach ?>
  65. </tbody>
  66. </table>
  67. <p>
  68. <?php for($i = 1 ;$i <= $pages;$i++): ?>
  69. <?php
  70. //!$_SERVER:服务器和执行环境信息,
  71. //!$_SERVER('PHP_SELF'):当前执行脚本的文件名
  72. $url = $_SERVER['PHP_SELF'] . '?p='.$i;
  73. $activa = $i == $_GET['p'] ? 'active' : null;
  74. ?>
  75. <a href="<?php echo $url ?>" class="<?php echo $activa ?>"><?php echo $i ?></a>
  76. <?php endfor ?>
  77. </p>
  78. </body>
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