Blogger Information
Blog 40
fans 0
comment 0
visits 27858
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP基础:引入命名空间、文件上传和分页
初见
Original
560 people have browsed it

引入命名空间

  • 命名空间
  1. /*作用:
  2. 1、use 引用彼得命名空间到当前空间
  3. 引用别的命名空间的类到当前空间
  4. 2、as给重复命名空间改别名
  5. */
  6. namespace app\admin\model
  7. {
  8. class Index{
  9. public static function index(){
  10. return '我是model空间里的Index类的index方法';
  11. }
  12. }
  13. }
  14. namespace app\admin\controller {
  15. class Index
  16. {
  17. public static function index()
  18. {
  19. return '我是one空间里的Index类的index方法';
  20. }
  21. }
  22. // 引入model后 app\admin\ 可以去掉
  23. // use \app\admin\model;
  24. // echo Index::index();
  25. // echo '<hr>';
  26. // echo model\Index::index();
  27. use \app\admin\model\Index as mIndex;
  28. echo mIndex::index();
  29. }
  • MVC
  1. //\index.php
  2. namespace box;
  3. require_once 'app\admin\autoload.php';
  4. //use 引入的命名空间要跟硬盘的目录结构对应上
  5. use \app\admin\controller\User;
  6. $user=new User();
  7. echo $user->index();
  8. //调用类或文件不存在救会抛出错误
  9. //系统异常:文件名不合法或不存在
  10. // \app\admin\autoload.php
  11. <?php
  12. spl_autoload_register(function($class){
  13. $file= str_replace('\\',DIRECTORY_SEPARATOR,$class). '.php';
  14. if(!is_file($file) || !file_exists($file)){
  15. try{
  16. throw new \Exception('文件名不合法或不存在');
  17. }catch (\Exception $e){
  18. echo '系统异常:'.$e->getMessage();
  19. }
  20. exit;
  21. }else{
  22. require_once $file;
  23. }
  24. });
  25. // \app\admin\controller\User.php
  26. <?php
  27. //命名空间也哦跟目录结构匹配
  28. namespace app\admin\controller;
  29. //require_once '../model/User.php';
  30. use app\admin\model\User as uM;
  31. class User
  32. {
  33. public function index()
  34. {
  35. echo uM::get_list();
  36. }
  37. }
  38. // \app\admin\model\User.php
  39. <?php
  40. namespace app\admin\model;
  41. class User
  42. {
  43. public static function get_list()
  44. {
  45. return 'model目录User get_list';
  46. }
  47. }

文件上传

  • 图片上传
  1. //单图
  2. //<form action="" method="post" enctype="multipart/form-data">
  3. //<input type="file" id="file" name="pic" />
  4. $des ='storage/';
  5. if(!file_exists($des)){
  6. mkdir($des,0770,true);//添加目录设置权限
  7. chmod($des,0700);//修改目录权限
  8. }
  9. $img_up =move_uploaded_file($_FILES['pic']['tmp_name'],$des.$_FILES['pic']['name']);
  10. if($img_up){
  11. $img=$des.$_FILES['pic']['name'];
  12. }else{
  13. $img=null;
  14. }
  15. //多图
  16. //<input type="file" id="file" name="pic[]" multiple />
  17. if(!empty($_FILES['pic'])){
  18. $des ='storage/';
  19. if(!file_exists($des)){
  20. mkdir($des,0770,true);//添加目录设置权限
  21. chmod($des,0700);//修改目录权限
  22. }
  23. $imgs =$_FILES['pic'];
  24. $pic =[];
  25. foreach ($imgs['name'] as $k=>$im) {
  26. $img_up =move_uploaded_file($imgs['tmp_name'][$k],$des.$im);
  27. $pic[] =$des.$im;
  28. }
  29. $img= implode(';',$pic);
  30. }
  • 封装上传
  1. function uploads($file,$des='./storge/',$flag=true){
  2. if(!empty($file)){
  3. if($file['error'] == 0){
  4. switch($file['error']) {
  5. case 1:
  6. return '文件大小超过了php.ini中允许的最大值,最大值是:'.ini_get('upload_max_filesize');
  7. case 2:
  8. return '文件大小超过了表单允许的最大值';
  9. case 3:
  10. return '只有部分文件上传';
  11. case 4:
  12. return '没有文件上传';
  13. case 6:
  14. return '找不到临时文件';
  15. case 7:
  16. return '文件写入失败';
  17. default:
  18. return '未知错误';
  19. }
  20. }else{
  21. if($flag){
  22. $arr = explode('.',$file['name']);
  23. $houzhui = ['jpg','gif','png'];
  24. if(!in_array($arr[1],$houzhui)){
  25. $res = '图片类型不对';
  26. }
  27. }
  28. if(!file_exists($des)){
  29. mkdir($des,0770,true);//添加目录设置权限
  30. chmod($des,0700);//修改目录权限
  31. }
  32. $imgs =$_FILES['pic'];
  33. $pic =[];
  34. foreach ($imgs['name'] as $k=>$im) {
  35. $file_name_new = date('YmdHis',time());
  36. $file_name_new .= rand(100,999);
  37. $img_up =move_uploaded_file($imgs['tmp_name'][$k],$des.$file_name_new);
  38. $pic[] =$des.$im;
  39. }
  40. $img= implode(';',$pic);
  41. }
  42. return $img;
  43. }
  44. return null;
  45. }

分页功能

  1. <?php
  2. session_start();// 开启会话
  3. //判断session
  4. if(empty($_SESSION['id'])){
  5. echo '<script>window.location.href="login.php";</script>';
  6. exit;
  7. }
  8. try{
  9. $pdo = new PDO('mysql:host=127.0.0.1;dbname=inbox','root','root');
  10. }catch(PDOException $e) {
  11. echo '数据库连接失败' . $e->getMessage();
  12. }
  13. if(empty($_GET['p'])){
  14. $p = 1;
  15. }else{
  16. $p = $_GET['p'];
  17. }
  18. $count = 5;
  19. $sql = 'SELECT * FROM `user` LIMIT ' . ($p-1)*$count . ',' . $count;
  20. $pre = $pdo -> prepare($sql);
  21. $exe = $pre -> execute();
  22. $data = $pre -> fetchAll();
  23. $pre = $pdo -> prepare('SELECT count(*) as totoal FROM `user`');
  24. $exe = $pre -> execute();
  25. $totoal = $pre -> fetch()['totoal'];
  26. $page = ceil($totoal/$count);
  27. ?>
  28. <!DOCTYPE html>
  29. <html lang="en">
  30. <head>
  31. <meta charset="UTF-8">
  32. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  33. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  34. <title>用户列表</title>
  35. <link rel="stylesheet" href="list.css">
  36. </head>
  37. <body>
  38. <div class="add"><a href="add.php">添加</a> <a href="loginout.php">退出</a></div>
  39. <table>
  40. <thead>
  41. <tr>
  42. <th width="50">ID</th>
  43. <th width="100">姓名</th>
  44. <th width="100">年龄</th>
  45. <th width="100">手机号</th>
  46. <th width="200">注册时间</th>
  47. <th width="200">登录时间</th>
  48. <th width="100">状态</th>
  49. <th width="100">修改 | 删除</th>
  50. </tr>
  51. </thead>
  52. <tbody>
  53. <?php
  54. foreach($data as $v){
  55. ?>
  56. <tr>
  57. <td><?=$v['id'] ?></td>
  58. <td><?=$v['name'] ?></td>
  59. <td><?=$v['age'].'岁' ?></td>
  60. <td><?=$v['tel'] ?></td>
  61. <td><?=$v['ctime'] ?></td>
  62. <td><?=date('Y-m-d H:i:s',$v['utime']) ?></td>
  63. <td>
  64. <?php
  65. if($v['status'] == 1){
  66. echo '<span style="color:green;">开启</span>';
  67. }else{
  68. echo '<span style="color:red;">关闭</span>';
  69. }
  70. ?></td>
  71. <td><a href="edit.php?id=<?=$v['id'] ?>">修改</a> | <a href="del.php?id=<?=$v['id'] ?>">删除</a></td>
  72. </tr>
  73. <!-- 这里要循环的html标签 -->
  74. <?php
  75. }
  76. ?>
  77. </tbody>
  78. </table>
  79. <ul class="pagination">
  80. <?php
  81. // 当前页面 等于1的话,就相当于没有上一页
  82. $html = '';
  83. if($p == 1){
  84. $html .= '<li class="disabled">';
  85. $html .= ' <a>上一页</a>';
  86. $html .= '</li>';
  87. }else{
  88. $html .= '<li>';
  89. $html .= ' <a href="lista.php?p='.($p-1).'">上一页</a>';
  90. $html .= '</li>';
  91. }
  92. // 总页10 是一个变量
  93. // 当前页 1,也是个变量
  94. for($i=1;$i<=$page;$i++){
  95. $html .= '<li ';
  96. if($i == $p){
  97. $html .= 'class="active"';
  98. }
  99. $html .= '>';
  100. $html .= ' <a href="lista.php?p='.$i.'">'.$i.'</a>';
  101. $html .= '</li>';
  102. }
  103. if($p == $page){
  104. $html .= '<li class="disabled">';
  105. $html .= ' <a>下一页</a>';
  106. $html .= '</li>';
  107. }else{
  108. $html .= '<li>';
  109. $html .= ' <a href="lista.php?p='.($p+1).'">下一页</a>';
  110. $html .= '</li>';
  111. }
  112. echo $html;
  113. ?>
  114. <!-- <div id="page" style="float:left;">-->
  115. <!-- <select id="pageNum" onchange="search()"-->
  116. <!-- style="width:50PX;margin-right:1px;" aria-controls="DataTables_Table_0" size="1" name="DataTables_Table_0_length">-->
  117. <!-- <option selected="selected" value="1">1</option>-->
  118. <!-- </select>-->
  119. <!-- </div>-->
  120. <div class="form-group">
  121. <select class="form-control" onchange="window.location=this.value;">
  122. <?php
  123. $pages ='';
  124. for($i=1;$i<=$page;$i++){
  125. $pages .= '<option ';
  126. if($i == $p){
  127. $pages .= 'selected="selected" ';
  128. }
  129. $pages .= 'value="lista.php?p='.$i.'">第'.$i.'页</option>';
  130. }
  131. echo $pages;
  132. ?>
  133. </select>
  134. </div>
  135. </ul>
  136. </body>
  137. </html>
  138. <script>
  139. function del(id){
  140. if(confirm("确定要删除该用户吗?")){
  141. this.location = "del.php?id=" + id;
  142. }else{
  143. return false;
  144. }
  145. }
  146. </script>

翻页

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