批改状态:合格
老师批语:
通过遍历$_FILES里面的数组实现多文件上传
把上传文件的代码封装称一个函数
<?phpfunction upload_error($error){switch ($error) {case 1:$msg = '超过的单文件大小';break;case 2:$msg = '超过前端表单限制';break;case 3:$msg = '上传文件不完整';break;case 4:$msg = '没有文件被上传';break;case 6:$msg = '找不到临时目录';break;case 7:$msg = '写文失败,目录无写入权限';break;default:$msg = '未定义错误';}return $msg;}function upload($files){if(is_array($files)){foreach($files['error'] as $key=> $error){if($error === 0){$patch='uploads/'.md5(strstr($files['name'][$key],'.',true)).strstr($files['name'][$key],'.');if(move_uploaded_file($files['tmp_name'][$key],$patch)){echo "<p> {$files['name'][$key]}上传成功</p>";echo "<img src='{$patch}' width='150'>";}else{echo upload_error($error);}}}}}
前端页面
<!-- 多文件批量上传 --><?php$files=$_FILES['files'];include 'common.php';echo upload($files);?><!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>Document</title></head><body><form action="" method="post" enctype="multipart/form-data"><fieldset><legend>多文件上传</legend><input type="file" name="files[]" multiple><button>上传</button></fieldset></form></body></html>
效果图
use PDO;
class Model{
public function getData(){
$pdo=new PDO(‘mysql:dbname=php’,’root’,’123456’);
$stmt=$pdo->prepare(‘select * from user limit 10’);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
视图```phpnamespace mvc;class View{public function fetch($data){$table='<table>';$table.=' <caption>员工列表</caption><thead><tr><th>id</th><th>姓名</th><th>年龄</th><th>性别</th><th>邮箱</th><th>生日</th><th>入职日期</th><th>操作</th></tr><tbody>';foreach ($data as $v){$table.='<tr>';$table.='<td>'. $v['id'].'</td>';$table.='<td>'. $v['name'].'</td>';$table.='<td>'. $v['age'].'</td>';$table.='<td>'. $v['gender'].'</td>';$table.='<td>'. $v['email'].'</td>';$table.='<td>'. $v['borthday'].'</td>';$table.='<td>'. $v['create_time'].'</td>';$table.='<td><button>编辑</button> <button>删除</button></td>';$table.='</tr>';}$table.='</tbody>';return $table;}}
控制器
namespace mvc;// 加载模型和视图require 'model.php';require 'view.php';class Controller1{public function index(){$md=new Model();$data=$md->getData();$view=new View();return $view->fetch($data);}}// 客户端测试// 创建控制器对象$controller=new Controller1();echo $controller->index();
效果图
解决对象之间的耦合
<?phpnamespace mvc;// 加载模型和视图require 'model.php';require 'view.php';class Controller2{public function index($md,$view){$data=$md->getData();return $view->fetch($data);}}// 客户端测试// 创建控制器对象$controller=new Controller2();$md=new Model();$view=new View();echo $controller->index($md,$view);
使用构造函数进行依赖注入
<?phpnamespace mvc;// 加载模型和视图require 'model.php';require 'view.php';class Controller3{private $model;private $view;public function __construct($model, $view){$this->model = $model;$this->view = $view;}public function index(){$data = $this->model -> getData();return $this->view->fetch($data);}}// 客户端测试// 创建控制器对象$md = new Model();$view = new View();$controller = new Controller3($md, $view);echo $controller->index();
将外部依赖对象进行统一管理
namespace mvc;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],[]);}}// 将依赖的外部对象添加到容器中$container=new Container();$container->bind('model',function(){return new Model();});$container->bind('view',function(){return new View();});class Controller4{public function index(Container $container){$data = $container->make('model')-> getData();return $container->make('view')->fetch($data);}}// 客户端测试$controller = new Controller4();echo $controller->index($container);

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