批改状态:合格
老师批语:
<?php// 多文件上传: 逐个上传printf('<pre>%s</pre>', print_r($_FILES, true));foreach ($_FILES as $file) {if ($file['error'] === 0) {$path = 'uploads/' . md5(strstr($file['name'] . 'helloworld', '.', true)) . strstr($file['name'], '.');if (move_uploaded_file($file['tmp_name'], $path)) {echo "<p style=\"color:violet\">{$file['name']}上传成功</p>";echo "<img src='{$path}' width='220' />";}} else {include 'bank.php';echo upload_error($error);}}?><!DOCTYPE html><html lang="zh-CN"><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>多文件上传</title></head><body><form action="" method="post" enctype="multipart/form-data"><fieldset><legend>多文件上传</legend><input type="hidden" name="MAX_FILE_SIZE" value="10485760"><input type="file" name="file1"><input type="file" name="file2"><button>上传</button></fieldset></form></body></html>
<?php// 函数库function upload_error(int $errno) : string{switch ($errno) {case 1:$msg = '超过的单文件大小';break;case 2:$msg = '超过前端表单限制';break;case 3:$msg = '上传文件不完整';break;case 4:$msg = '没有文件被上传';break;case 6:$msg = '找不到临时目录';break;case 7:$msg = '写入失败,目标目录无写入权限';default:exit('未定义错误');}return $msg;}


<?php// 多文件上传: 批量上传,允许同时选择多个printf('<pre>%s</pre>', print_r($_FILES, true));$files = $_FILES['files'] ?? null;foreach ($files['error'] as $key => $error) {if ($error === 0) {$path = 'uploads/' . md5(strstr($files['name'][$key] . 'helloworld', '.', true)) . strstr($files['name'][$key], '.');if (move_uploaded_file($files['tmp_name'][$key], $path)) {echo "<p style=\"color:violet\">{$files['name'][$key]}上传成功</p>";echo "<img src='{$path}' width='260' />";}} else {include 'bank.php';echo upload_error($error);}}?><!DOCTYPE html><html lang="zh-CN"><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>多文件上传</title></head><body><form action="" method="post" enctype="multipart/form-data"><fieldset><legend>多文件上传</legend><input type="hidden" name="MAX_FILE_SIZE" value="10485760"><!-- 添加 multipart name的值写成php数组的格式 --><input type="file" multiple name="files[]"><button>上传</button></fieldset></form></body></html>


先创建模型model.php,再建立视图view.php,最后运行容器controller1.php
<?phpnamespace 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 $b) {die('连接失败:' . $b->getMessage());}$sql = "SELECT id, name, gender,salary,email FROM staffs limit 4";$stmt = $pdo->prepare($sql);$stmt->execute();return $stmt->fetchAll();}}
<?phpnamespace mvc_demo;// 视图class View{// 数据显示public function fetch($datab){$nob = '';foreach ($datab as $data) {$nob .= print_r($data, true) . '<br>';}return $nob;}}
<?phpnamespace mvc_demo;use Closure;// 加载模型require 'model.php';// 加载视图require 'view.php';// 服务容器class Container{// 对象容器protected $instances = [];// 添加容器public function bind($object, Closure $process){$this->instances[$object] = $process;}// 取出对象public function make($object, $params = []){return call_user_func_array($this->instances[$object], $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号