PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

博客列表 > 实现多文件上传案例 以及 MVC与依赖注入的原理

实现多文件上传案例 以及 MVC与依赖注入的原理

lus菜
lus菜 原创
2021年03月08日 11:58:37 754浏览

一. 多文件上传并将其封装成可复用的函数或者类:

1.逐个上传样式代码:

  1. <?php
  2. // 多文件上传: 逐个上传
  3. printf('<pre>%s</pre>', print_r($_FILES, true));
  4. foreach ($_FILES as $file) {
  5. if ($file['error'] === 0) {
  6. $path = 'uploads/' . md5(strstr($file['name'] . 'helloworld', '.', true)) . strstr($file['name'], '.');
  7. if (move_uploaded_file($file['tmp_name'], $path)) {
  8. echo "<p style=\"color:violet\">{$file['name']}上传成功</p>";
  9. echo "<img src='{$path}' width='220' />";
  10. }
  11. } else {
  12. include 'bank.php';
  13. echo upload_error($error);
  14. }
  15. }
  16. ?>
  17. <!DOCTYPE html>
  18. <html lang="zh-CN">
  19. <head>
  20. <meta charset="UTF-8">
  21. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  22. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  23. <title>多文件上传</title>
  24. </head>
  25. <body>
  26. <form action="" method="post" enctype="multipart/form-data">
  27. <fieldset>
  28. <legend>多文件上传</legend>
  29. <input type="hidden" name="MAX_FILE_SIZE" value="10485760">
  30. <input type="file" name="file1">
  31. <input type="file" name="file2">
  32. <button>上传</button>
  33. </fieldset>
  34. </form>
  35. </body>
  36. </html>

bank.php函数库样式代码:

  1. <?php
  2. // 函数库
  3. function upload_error(int $errno) : string
  4. {
  5. switch ($errno) {
  6. case 1:
  7. $msg = '超过的单文件大小';
  8. break;
  9. case 2:
  10. $msg = '超过前端表单限制';
  11. break;
  12. case 3:
  13. $msg = '上传文件不完整';
  14. break;
  15. case 4:
  16. $msg = '没有文件被上传';
  17. break;
  18. case 6:
  19. $msg = '找不到临时目录';
  20. break;
  21. case 7:
  22. $msg = '写入失败,目标目录无写入权限';
  23. default:
  24. exit('未定义错误');
  25. }
  26. return $msg;
  27. }

多文件逐个上传效果预览:


2.多文件上传允许同时选择多个样式代码:

  1. <?php
  2. // 多文件上传: 批量上传,允许同时选择多个
  3. printf('<pre>%s</pre>', print_r($_FILES, true));
  4. $files = $_FILES['files'] ?? null;
  5. foreach ($files['error'] as $key => $error) {
  6. if ($error === 0) {
  7. $path = 'uploads/' . md5(strstr($files['name'][$key] . 'helloworld', '.', true)) . strstr($files['name'][$key], '.');
  8. if (move_uploaded_file($files['tmp_name'][$key], $path)) {
  9. echo "<p style=\"color:violet\">{$files['name'][$key]}上传成功</p>";
  10. echo "<img src='{$path}' width='260' />";
  11. }
  12. } else {
  13. include 'bank.php';
  14. echo upload_error($error);
  15. }
  16. }
  17. ?>
  18. <!DOCTYPE html>
  19. <html lang="zh-CN">
  20. <head>
  21. <meta charset="UTF-8">
  22. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  23. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  24. <title>多文件上传</title>
  25. </head>
  26. <body>
  27. <form action="" method="post" enctype="multipart/form-data">
  28. <fieldset>
  29. <legend>多文件上传</legend>
  30. <input type="hidden" name="MAX_FILE_SIZE" value="10485760">
  31. <!-- 添加 multipart name的值写成php数组的格式 -->
  32. <input type="file" multiple name="files[]">
  33. <button>上传</button>
  34. </fieldset>
  35. </form>
  36. </body>
  37. </html>

效果预览:


二. MVC与依赖注入的原理,以及服务容器对依赖对象的管理实现过程

先创建模型model.php,再建立视图view.php,最后运行容器controller1.php

model.php样式代码:

  1. <?php
  2. namespace mvc_demo;
  3. use PDO;
  4. // 模型类
  5. class Model
  6. {
  7. // 获取数据
  8. public function getData()
  9. {
  10. try {
  11. $pdo = new PDO('mysql:host=localhost;dbname=phpedu;charset=utf8mb4', 'root', 'root');
  12. // 设置结果集的返回类型
  13. $pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
  14. } catch (\PDOException $b) {
  15. die('连接失败:' . $b->getMessage());
  16. }
  17. $sql = "SELECT id, name, gender,salary,email FROM staffs limit 4";
  18. $stmt = $pdo->prepare($sql);
  19. $stmt->execute();
  20. return $stmt->fetchAll();
  21. }
  22. }

view.php样式代码:

  1. <?php
  2. namespace mvc_demo;
  3. // 视图
  4. class View
  5. {
  6. // 数据显示
  7. public function fetch($datab)
  8. {
  9. $nob = '';
  10. foreach ($datab as $data) {
  11. $nob .= print_r($data, true) . '<br>';
  12. }
  13. return $nob;
  14. }
  15. }

controller1.php样式代码:

  1. <?php
  2. namespace mvc_demo;
  3. use Closure;
  4. // 加载模型
  5. require 'model.php';
  6. // 加载视图
  7. require 'view.php';
  8. // 服务容器
  9. class Container
  10. {
  11. // 对象容器
  12. protected $instances = [];
  13. // 添加容器
  14. public function bind($object, Closure $process)
  15. {
  16. $this->instances[$object] = $process;
  17. }
  18. // 取出对象
  19. public function make($object, $params = [])
  20. {
  21. return call_user_func_array($this->instances[$object], $params);
  22. }
  23. }
  24. // 依赖的对象添加到容器中
  25. $container = new Container();
  26. $container->bind('model', function () {
  27. return new Model();
  28. });
  29. $container->bind('view', function () {
  30. return new View();
  31. });
  32. // 控制器
  33. // 容器获取数据
  34. class Controller
  35. {
  36. public function index(Container $container)
  37. {
  38. // 获取数据
  39. $datas = $container->make('model')->getData();
  40. // 渲染数据
  41. return $container->make('view')->fetch($datas);
  42. }
  43. }
  44. $controller = new Controller();
  45. echo $controller->index($container);

效果预览:

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议