批改状态:合格
老师批语:
- 写一个实现的多文件上传案例,并将它封装成一个可复用的函数或类
- 实例演示MVC与依赖注入的原理,以及服务容器对依赖对象的管理的实现过程。
<?php// 多文件上传1function upload_multi_file1($names = ['file1', 'file2']){foreach ($_FILES as $key => $file) {if (in_array($key, $names)) {if ($file['error']) {echo upload_err_msg($file['error']);} else {$filename = strstr($file['name'], '.', true);$extname = strstr($file['name'], '.');$path = 'uploads/' . md5($filename . time()) . $extname;if (move_uploaded_file($file['tmp_name'], $path)) {echo "<p>上传成功 {$file['name']}</p>";echo "<p><img src=\"{$path}\" width=\"150\"></p>";}}}}}// 多文件上传2,一次选中多个function upload_multi_file2($name = 'files'){if (isset($_FILES[$name]) && $_FILES[$name]['error']) {foreach ($_FILES[$name]['error'] as $key => $err) {if ($err) {echo upload_err_msg($err);} else {$filename = strstr($_FILES[$name]['name'][$key], '.', true);$extname = strstr($_FILES[$name]['name'][$key], '.');$path = 'uploads/' . md5($filename . time()) . $extname;if (move_uploaded_file($_FILES[$name]['tmp_name'][$key], $path)) {echo "<p>上传成功 {$_FILES[$name]['name'][$key]}</p>";echo "<p><img src=\"{$path}\" width=\"150\"></p>";}}}}}// 上传错误消息function upload_err_msg($err = 0){$err_msg = [0 => '上传完成',1 => '超过的单文件大小',2 => '超过前端表单限制',3 => '上传文件不完整',4 => '没有文件被上传',5 => '系统保留',6 => '找不到临时目录',7 => '目标没有写入权限',];if (isset($err_msg[$err])) return $err_msg[$err];return '未定义的错误';}// 多文件上传upload_multi_file1($names = ['file1', 'file2']);upload_multi_file2($name = 'files');?><!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title></head><body><form action="" method="POST" enctype="multipart/form-data"><fieldset><legend>多文件上传1</legend><input type="file" name="file1"><input type="file" name="file2"><button>上传</button></fieldset></form><form action="" method="POST" enctype="multipart/form-data"><fieldset><legend>多文件上传2</legend><input type="hidden" name="MAX_FILE_SIZE" value="10485760"><input type="file" multiple name="files[]"><button>上传</button></fieldset></form></body></html>


namespace mvc_demo;use PDO;// 模型类class Model{// 获取数据public function getData(){try {$pdo = new PDO('mysql:host=localhost;dbname=phpedu;charset=utf8mb4', 'root', 'root');// 设置结果集的返回类型$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);} catch (\PDOException $e) {die('连接失败:' . $e->getMessage());}$sql = "SELECT id, name, gender FROM staffs limit 5";$stmt = $pdo->prepare($sql);$stmt->execute();return $stmt->fetchAll();}}
namespace mvc_demo;// 视图class View{// 数据展示public function fetch($datas){$res = '';foreach ($datas as $data) {$res .= print_r($data, true) . '<br>';}return $res;}}
namespace mvc_demo;use Closure;// 加载模型require 'model.php';// 加载视图require 'view.php';// 服务容器class Container{// 对象容器protected $instances = [];// 添加容器public function bind($alias, Closure $process){$this->instances[$alias] = $process;}// 取出对象public function make($alias, $params = []){return call_user_func_array($this->instances[$alias], $params);}}// 依赖的对象添加到容器中$container = new Container();$container->bind('model', function () {return new Model();});$container->bind('view', function () {return new View();});// 控制器// 容器获取数据class Controller{public function index(Container $container){// 获取数据$datas = $container->make('model')->getData();// 渲染数据return $container->make('view')->fetch($datas);}}$controller = new Controller();echo $controller->index($container);

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号