1. 多文件上传、封装成可复用的函数或类
- 运行截图:
// 多文件上传
function upload_muifile($name = 'files') {
if (isset($_FILES[$name]) && $_FILES[$name]['error']) {
foreach ($_FILES[$name]['error'] as $key => $error) {
if ($error) {
echo "<p style='color:red;'>" . upload_ero($error) . "</p>";
} else {
$filename = strstr($_FILES[$name]['name'][$key],'.',true);
$ext = strstr($_FILES[$name]['name'][$key],'.');
$path = 'uploads/' . md5($filename . 'hello') . $ext;
if (move_uploaded_file($_FILES[$name]['tmp_name'][$key],$path)) {
echo "<p style='color:green;'>{$_FILES[$name]['name'][$key]},上传成功!</p>";
echo "<img src='{$path}' width='150'/>";
}
}
}
}
}
// 上传错误编码
function upload_ero(int $error = 0) : string
{
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:
exit('未定义错误!');
}
return $msg;
}
// 调用上传文件函数
upload_muifile($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>多文件上传</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" multiple name="files[]">
<button>上传</button>
</fieldset>
</form>
</body>
</html>
—
2. MVC与依赖注入、服务容器对依赖对象的管理
- 控制器:Controller.php运行截图
- 模型:Model.php
namespace mvc_user;
use PDO;
// 模型类
class Model
{
// 获取数据
public function get_data()
{
$pdo = new PDO('mysql:dbname=phpedu','root','123456');
$stmt = $pdo->prepare('select * from users limit 10');
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
- 视图:View.php
namespace mvc_user;
// 视图
class View
{
// 数据显示
public function fetch($data)
{
$table = '<table border="1" cellspacing="0">';
$table .= '<caption>用户信息表</caption>
<tr bgcolor="lightyellow">
<td>UID</td>
<td>姓名</td>
<td>性别</td>
<td>工资</td>
<td>邮箱</td>
<td>生日</td>
</tr>';
foreach ($data as $user) {
$table .= '<tr>';
$table .= '<td>' . $user['sid'] . '</td>';
$table .= '<td>' . $user['name'] . '</td>';
$table .= '<td>' . $user['gender'] . '</td>';
$table .= '<td>' . $user['salary'] . '</td>';
$table .= '<td>' . $user['email'] . '</td>';
$table .= '<td>' . $user['birthday'] . '</td>';
$table .= '</tr>';
}
$table .= '</table>';
return $table;
}
}
- 控制器:Controller.php
namespace mvc_user;
use Closure;
require 'Model.php';
require 'View.php';
// 服务容器
class Cont1
{
// 对象容器
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],[]);
}
}
// 将依赖的对象添加到容器中
$cont = new Cont1();
$cont->bind('model',function () {return new Model();});
$cont->bind('view',function () {return new view();});
class Controller4
{
public function index(Cont1 $cont)
{
$data = $cont->make('model')->get_data();
return $cont->make('view')->fetch($data);
}
}
$controller = new Controller4();
echo $controller->index($cont);