作业内容:1. 实例演示单文件上传与多文件上传

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. // $_FILES: PHP超全局变量数量, 保存着上传文件的全部信息
  5. //printf('<pre>%s</pre>',print_r($_FILES,true));
  6. if(isset($_FILES['mypic'])){
  7. $filename = $_FILES['mypic']['name'];
  8. $tmpName = $_FILES['mypic']['tmp_name'];
  9. $error = $_FILES['mypic']['error'];
  10. if($error > 0){
  11. //echo "文件上传失败<br>";
  12. $tips = '<span style="color:red">上传失败:</span>';
  13. switch($error){
  14. case 1:
  15. $tips.= '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值。 ';
  16. break;
  17. case 2:
  18. $tips.= '上传文件的大小超过了 HTML 表单中 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 $tips;
  34. }else{
  35. if(is_uploaded_file($tmpName)){
  36. //添加白名单数组
  37. $whitelist = ['jpg','jpeg','png','gif','bmp'];
  38. //Returns information about a file path
  39. $filetype = pathinfo($filename)['extension'];
  40. //Checks if a value exists in an array
  41. if(in_array($filetype,$whitelist)){
  42. $path = 'myfile/';
  43. $target = $path .md5($filename) . ".{$filetype}";
  44. if(move_uploaded_file($tmpName,$target)){
  45. echo "<p style='color:green'>文件上传成功</p><br>";
  46. echo "<img src='$target' width='150px'>";
  47. }else{
  48. echo "<p style='color:red'>文件移动失败</p><br>";
  49. }
  50. }else{
  51. echo "<p style='color:red'>文件格式错误</p><br>";
  52. }
  53. }else{
  54. echo "<p style='color:red'>非法文件</p><br>";
  55. }
  56. }
  57. }
  58. /**
  59. * 1. $_FILES: 二维数组,每个元素对应一个上传的文件
  60. * 2. name: 原始文件名
  61. * 3. type: 文件类型, mime类型
  62. * 4. tmp_name: 临时目录
  63. * 5. error: 错误代码
  64. * 5. size: 文件大小(字节表示 byte)
  65. */
  66. ?>
  67. <head>
  68. <meta charset="UTF-8">
  69. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  70. <title>Document</title>
  71. </head>
  72. <body>
  73. <form action="" method="POST" enctype="multipart/form-data">
  74. <fieldset>
  75. <legend>单文件上传</legend>
  76. <input type="hidden" name="MAX_FILE_SIZE" value="300000">
  77. <input type="file" name="mypic">
  78. <button>上传文件</button>
  79. </fieldset>
  80. </form>
  81. </body>
  82. </html>

  1. if(isset($_FILES['mypic'])){
  2. foreach($_FILES['mypic']['error'] as $key=>$error){
  3. if($error == 0){
  4. $tmpName = $_FILES['mypic']['tmp_name'][$key];
  5. $filename = $_FILES['mypic']['name'][$key];
  6. if(is_uploaded_file($tmpName)){
  7. //添加白名单数组
  8. $whitelist = ['jpg','jpeg','png','gif','bmp'];
  9. //Returns information about a file path
  10. $filetype = pathinfo($filename)['extension'];
  11. //Checks if a value exists in an array
  12. if(in_array($filetype,$whitelist)){
  13. $path = 'myfile/';
  14. $target = $path .md5($filename) . ".{$filetype}";
  15. if(move_uploaded_file($tmpName,$target)){
  16. echo "<p style='color:green'>文件上传成功</p><br>";
  17. echo "<img src='$target' width='150px'>";
  18. }else{
  19. echo "<p style='color:red'>文件移动失败</p><br>";
  20. }
  21. }else{
  22. echo "<p style='color:red'>文件格式错误</p><br>";
  23. }
  24. }else{
  25. echo "<p style='color:red'>非法文件</p><br>";
  26. }
  27. }
  28. <input type="file" name="mypic[]" multiple>
  1. 实例演示简单分页操作,并理解分页所需参数的意思

  1. <body>
  2. <table>
  3. <caption>员工表</caption>
  4. <thead>
  5. <th>ID</th>
  6. <th>部门</th>
  7. <th>姓名</th>
  8. <th>级别</th>
  9. </thead>
  10. <tbody>
  11. <?php
  12. foreach($res as $user){
  13. extract($user);
  14. echo <<<data
  15. <tr>
  16. <td>$id</td>
  17. <td>$dept</td>
  18. <td>$name</td>
  19. <td>$grade</td>
  20. </tr>
  21. data;
  22. }
  23. ?>
  24. </tbody>
  25. </table>
  26. <nav>
  27. <?php
  28. $dir = $_SERVER['PHP_SELF'];
  29. for($i=1;$i<=$pagenum;$i++){
  30. echo <<<nav
  31. <a href="$dir?id=$i">$i</a>
  32. nav;
  33. }
  34. ?>
  35. </nav>
  36. </body>

数据获取部分

  1. <?php
  2. //1、连接数据库
  3. //require __DIR__.'/hw.php';
  4. //use PDO;
  5. $dbConfig =[
  6. 'type'=>'mysql',
  7. 'host' => 'localhost',
  8. 'dbname' => 'testsql',
  9. 'port'=>'3306',
  10. 'charset'=>'utf8',
  11. 'username' => 'zolo',
  12. 'password' => '123456'
  13. ];
  14. //import variables into the current symbol table from an array
  15. extract($dbConfig);
  16. $db = new PDO("$type:host=$host;port=$port;dbname=$dbname;charset=$charset",$username,$password);
  17. // $page = isset($_GET['id'])?$_GET['id']:1;
  18. $page = $_GET['id']??1;
  19. //查询操作
  20. //单条查询
  21. $sqlsumnum = "select count(*) as `sumnum` from `users`";
  22. //$stmt = $db->prepare($sqlquery);
  23. $stmt = $db->prepare($sqlsumnum);
  24. $stmt ->execute();
  25. //多条查询
  26. $stmt ->bindColumn('sumnum',$sumnum);
  27. $stmt->fetch(PDO::FETCH_ASSOC);
  28. $num = 5;
  29. $pagenum = floor($sumnum/$num);
  30. $index = ($page-1)*$num;
  31. //echo "当前页面偏移量:$index<br>";
  32. //echo "$sumnum<br>$pagenum<br>";
  33. $sqlquery = "select * from `users` limit $index,$num";
  34. $stmt = $db->prepare($sqlquery);
  35. $stmt ->execute();
  36. $res = $stmt->fetchAll(PDO::FETCH_ASSOC);
  37. //echo "批量查询成功:<br>";
  38. foreach($res as $user){
  39. //printf('<pre>%s</pre>',print_r($user,true));
  40. extract($user);}
  41. ?>

更多相关文章

  1. [转]Android高手进阶教程(四)之----Android(安卓)中自定义属性(a
  2. Android(安卓)Button及TextView动态变换颜色
  3. Android(安卓)RadioButton设置选中时文字和背景颜色同时改变
  4. Android(安卓)中自定义属性(attr.xml,TypedArray)的使用!
  5. Android(安卓)NDK编程中Application.mk和Android.mk写法
  6. Android(安卓)文件路径详解
  7. 从头学Android之Android的数据存储--File
  8. 第二章 Android动态加载、热更新、热修复、插件化系列文章 之 认
  9. 详解高速神器python脚步打包android apk,超级快!!(打包系列教程之六)

随机推荐

  1. mysql-proxy主从搭建读写分离全过程
  2. MyBatis排序时使用order by 动态参数时需
  3. [Step By Step]使用SAP Business Objects
  4. 如何在表单行SQL中检查是否有任何值为Nul
  5. 麻烦问下,为什么我的mysql存储过程一次调
  6. Ruby 1.9 + MySQL中发生访问错误
  7. MySql索引的优缺点
  8. [转]swoole安装报错详解 mysqlnd_find_ch
  9. 我需要介绍MongoDB / NoSQL数据库
  10. MySQL数据库辅助类