Correcting teacher:天蓬老师
Correction status:qualified
Teacher's comments:
<?php
printf('<pre>%s</pre>', print_r($_FILES, true));
if (count($_FILES) > 0) {
include './common.php';
switch ($_GET['t']) {
case 'alone':
echo upload($_FILES['file']);
break;
case 'onebyone':
foreach ($_FILES as $file) {
// echo $file['name'];
echo upload($file);
}
break;
case 'multipart':
$files = [];
foreach ($_FILES['file']['error'] as $key => $value) {
$files[] = [
'name' => $_FILES['file']['name'][$key],
'type' => $_FILES['file']['type'][$key],
'tmp_name' => $_FILES['file']['tmp_name'][$key],
'error' => $_FILES['file']['error'][$key],
'size' => $_FILES['file']['size'][$key],
];
}
foreach ($files as $file) {
// echo $file['name'];
echo upload($file);
}
// var_dump($files);
break;
default:
echo '未知错误';
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>图片上传</title>
<style>
form {
width: 20em;
margin: 1em auto;
}
fieldset {
margin: 1em;
display: grid;
place-content: flex-start;
}
button[type=button] {
margin-bottom: .5em;
}
</style>
</head>
<body>
<form action="upload.php?t=alone" method="post" enctype="multipart/form-data">
<fieldset>
<label>单文件上传</label>
<input type="hidden" name="MAX_FILE_SIZE" value="10485760">
<input type="file" name="file">
<button>upload</button>
</fieldset>
</form>
<form action="upload.php?t=onebyone" method="post" enctype="multipart/form-data">
<fieldset>
<label>多文件上传: 逐个上传</label>
<input type="hidden" name="MAX_FILE_SIZE" value="10485760">
<input type="file" name="file1">
<button type="button" onclick="oneByOne()">继续添加文件</button>
<button>upload</button>
</fieldset>
</form>
<form action="upload.php?t=multipart" method="post" enctype="multipart/form-data">
<fieldset>
<label>多文件上传: 一次性上传</label>
<input type="hidden" name="MAX_FILE_SIZE" value="10485760">
<input type="file" name="file[]" multiple>
<button>upload</button>
</fieldset>
</form>
<script>
let oneByOneFileNameIndex = 1;
// 动态添加逐个上传的input
function oneByOne() {
const dom = document.querySelector(`input[name=file${oneByOneFileNameIndex}]`);
const input = document.createElement('input');
input.type = 'file'
input.name = 'file' + ++oneByOneFileNameIndex;
dom.insertAdjacentElement('afterend', input);
}
</script>
</body>
</html>
<?php
/**
* 文件上传
*
* @param array $files: 一个文件的所有数据
* @return string
*/
function upload(array $files): string
{
// 获取文件名
$name = $files['name'];
// 获取临时文件的路径
$tmpName = $files['tmp_name'];
// 获取错误信息
$error = $files['error'];
if ($error === 0) {
// 获取文件类型
$type = strstr($files['type'], '/', true);
// 获取文件后缀
$ext = '.' . strtolower(pathinfo($files['name'], PATHINFO_EXTENSION));
if ($type === 'image') {
$path = "uploads/" . md5($name . date('Ymd') . str_pad(mt_rand(1, 9999), 5, '0', STR_PAD_LEFT)) . $ext;
// 检测临时文件的合法性
if (is_uploaded_file($tmpName)) {
// 移动文件
if (move_uploaded_file($tmpName, $path)) {
// 返回成功
return "<span><p style=\"color:green\">{$name}上传成功</p><img src='{$path}' width=250 alt='{$name}' /></span>";
} else {
return '移动文件失败';
}
} else {
return '临时文件不合法';
}
} else {
return '上传类型错误';
}
} else {
return upload_error($error);
}
}
/**
* 文件上传的错误信息集合
*
* @param int $errno: error代码
* @return string
*/
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 = '写入失败,目标目录无写入权限';
break;
default:
exit('未定义错误');
}
return $msg;
}
MVC:
M-模型(数据库相关的操作),
V-视图(页面,html),
C-控制器(功能:1.获取数据 2.选择视图)
依赖注入: 简单那来说就是为了解决对象之间的耦合(`在一个类中调用其他类的实例`),例2.1:
方法: 在类的外部实例化其他类, 然后以参数的方式传入.例:2.2:
服务容器: 将外部依赖(需要引用的外部类实例)进行统一的管理.例2.3
<?php
namespace test;
require './Model.php';
require './View.php';
// 服务容器
class Conn
// 控制器
class Controller
{
public function index(): string
{
return (new View())->fetch((new \test\Model())->select()['data']);
}
}
<?php
namespace test;
require './Model.php';
require './View.php';
// 控制器
class Controller
{
// public function index(): string
// {
// return (new View())->fetch((new \test\Model())->select()['data']);
// }
protected $model;
protected $view;
public function __construct($model,$view) {
$this->model = $model;
$this->view = $view;
}
public function index(): string
{
return $this->view->fetch($this->model->select()['data']);
}
}
$model = new Model();
$view = new View();
$controller = new Controller($model, $view);
echo $controller->index();
<?php
namespace test;
require './Model.php';
require './View.php';
// 服务容器
class Container
{
// 对象容器
protected $instanles = [];
// 写入对象
public function bind($alias, \Closure $process)
{
$this->instanles[$alias] = $process;;
}
// 取出对象
public function make($alias, $params = [])
{
return call_user_func_array($this->instanles[$alias], $params);
}
}
// 控制器
class Controller
{
/*服务容器*/
public function index(Container $instanles)
{
return $instanles->make('view')->fetch($instanles->make('model')->select()['data']);
}
}
<?php
namespace mvc_demo;
use PDO;
// 模型类
class Model
{
// 获取数据
public function getData()
{
$pdo = new PDO('mysql:dbname=phpedu', 'root', 'root');
$stmt = $pdo->prepare('select * from staffs limit 10');
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
<?php
namespace mvc_demo;
// 视图
class View
{
// 数据展示
public function fetch($data)
{
$table = '<table border="1" cellspacing="0">';
$table .= '<caption>员工信息表</caption>
<tr bgcolor="lightcyan">
<th>id</th>
<td>姓名</td>
<td>性别</td>
<td>工资</td>
<td>邮箱</td>
<td>生日</td>
</tr>';
foreach ($data as $staff) {
$table .= '<tr>';
$table .= '<td>' . $staff['sid'] . '</td>';
$table .= '<td>' . $staff['name'] . '</td>';
$table .= '<td>' . $staff['gender'] . '</td>';
$table .= '<td>' . $staff['salary'] . '</td>';
$table .= '<td>' . $staff['email'] . '</td>';
$table .= '<td>' . $staff['birthday'] . '</td>';
$table .= '</tr>';
}
$table .= '</table>';
return $table;
}
}
<?php
namespace test;
require './Model.php';
require './View.php';
// 服务容器
class Container
{
// 对象容器
protected $instanles = [];
// 写入对象
public function bind($alias, \Closure $process)
{
$this->instanles[$alias] = $process;;
}
// 取出对象
public function make($alias, $params = [])
{
return call_user_func_array($this->instanles[$alias], $params);
}
}
// 控制器
class Controller
{
// public function index(): string
// {
// return (new View())->fetch((new \test\Model())->select()['data']);
// }
/*依赖注入*/
// protected $model;
// protected $view;
//
// public function __construct($model, $view)
// {
// $this->model = $model;
// $this->view = $view;
// }
//
// public function index(): string
// {
// return $this->view->fetch($this->model->select()['data']);
// }
/*服务容器*/
public function index(Container $instanles)
{
return $instanles->make('view')->fetch($instanles->make('model')->select()['data']);
}
}
<?php
/*服务容器*/
require './Controller.php';
$container = new \test\Container();
$container->bind('model', function (){return new \test\Model();});
$container->bind('view', function (){return new \test\View();});
$table = new \test\Controller();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>mvc</title>
</head>
<body>
<?php echo $table->index($container);?>
</body>
</html>