Blogger Information
Blog 47
fans 3
comment 0
visits 38221
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
多文件上传、MVC依赖注入与服务容器
Original
776 people have browsed it

1. 多文件上传、封装成可复用的函数或类

  • 运行截图:

  1. // 多文件上传
  2. function upload_muifile($name = 'files') {
  3. if (isset($_FILES[$name]) && $_FILES[$name]['error']) {
  4. foreach ($_FILES[$name]['error'] as $key => $error) {
  5. if ($error) {
  6. echo "<p style='color:red;'>" . upload_ero($error) . "</p>";
  7. } else {
  8. $filename = strstr($_FILES[$name]['name'][$key],'.',true);
  9. $ext = strstr($_FILES[$name]['name'][$key],'.');
  10. $path = 'uploads/' . md5($filename . 'hello') . $ext;
  11. if (move_uploaded_file($_FILES[$name]['tmp_name'][$key],$path)) {
  12. echo "<p style='color:green;'>{$_FILES[$name]['name'][$key]},上传成功!</p>";
  13. echo "<img src='{$path}' width='150'/>";
  14. }
  15. }
  16. }
  17. }
  18. }
  19. // 上传错误编码
  20. function upload_ero(int $error = 0) : string
  21. {
  22. switch ($error) {
  23. case 1:
  24. $msg = '超过单文件大小!';
  25. break;
  26. case 2:
  27. $msg = '超过前端表单限制';
  28. break;
  29. case 3:
  30. $msg = '上传文件不完整';
  31. break;
  32. case 4:
  33. $msg = '没有文件被上传';
  34. break;
  35. case 6:
  36. $msg = '找不到临时目录';
  37. break;
  38. case 7:
  39. $msg = '写入失败,请检查目标目录权限';
  40. break;
  41. default:
  42. exit('未定义错误!');
  43. }
  44. return $msg;
  45. }
  46. // 调用上传文件函数
  47. upload_muifile($name = 'files');
  48. ?>
  49. <!DOCTYPE html>
  50. <html lang="en">
  51. <head>
  52. <meta charset="UTF-8">
  53. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  54. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  55. <title>多文件上传</title>
  56. </head>
  57. <body>
  58. <form action="" method="post" enctype="multipart/form-data">
  59. <fieldset>
  60. <legend>多文件上传</legend>
  61. <input type="hidden" name="MAX_FILE_SIZE" value="10485760">
  62. <input type="file" multiple name="files[]">
  63. <button>上传</button>
  64. </fieldset>
  65. </form>
  66. </body>
  67. </html>

2. MVC与依赖注入、服务容器对依赖对象的管理

  • 控制器:Controller.php运行截图

  • 模型:Model.php
  1. namespace mvc_user;
  2. use PDO;
  3. // 模型类
  4. class Model
  5. {
  6. // 获取数据
  7. public function get_data()
  8. {
  9. $pdo = new PDO('mysql:dbname=phpedu','root','123456');
  10. $stmt = $pdo->prepare('select * from users limit 10');
  11. $stmt->execute();
  12. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  13. }
  14. }
  • 视图:View.php
  1. namespace mvc_user;
  2. // 视图
  3. class View
  4. {
  5. // 数据显示
  6. public function fetch($data)
  7. {
  8. $table = '<table border="1" cellspacing="0">';
  9. $table .= '<caption>用户信息表</caption>
  10. <tr bgcolor="lightyellow">
  11. <td>UID</td>
  12. <td>姓名</td>
  13. <td>性别</td>
  14. <td>工资</td>
  15. <td>邮箱</td>
  16. <td>生日</td>
  17. </tr>';
  18. foreach ($data as $user) {
  19. $table .= '<tr>';
  20. $table .= '<td>' . $user['sid'] . '</td>';
  21. $table .= '<td>' . $user['name'] . '</td>';
  22. $table .= '<td>' . $user['gender'] . '</td>';
  23. $table .= '<td>' . $user['salary'] . '</td>';
  24. $table .= '<td>' . $user['email'] . '</td>';
  25. $table .= '<td>' . $user['birthday'] . '</td>';
  26. $table .= '</tr>';
  27. }
  28. $table .= '</table>';
  29. return $table;
  30. }
  31. }
  • 控制器:Controller.php
  1. namespace mvc_user;
  2. use Closure;
  3. require 'Model.php';
  4. require 'View.php';
  5. // 服务容器
  6. class Cont1
  7. {
  8. // 对象容器
  9. protected $instances = [];
  10. // 添加对象
  11. public function bind($alias,Closure $process)
  12. {
  13. $this->instances[$alias] = $process;
  14. }
  15. // 取出对象
  16. public function make($alias,$params=[]){
  17. return call_user_func_array($this->instances[$alias],[]);
  18. }
  19. }
  20. // 将依赖的对象添加到容器中
  21. $cont = new Cont1();
  22. $cont->bind('model',function () {return new Model();});
  23. $cont->bind('view',function () {return new view();});
  24. class Controller4
  25. {
  26. public function index(Cont1 $cont)
  27. {
  28. $data = $cont->make('model')->get_data();
  29. return $cont->make('view')->fetch($data);
  30. }
  31. }
  32. $controller = new Controller4();
  33. echo $controller->index($cont);
Correcting teacher:天蓬老师天蓬老师

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