实现多文件上传案例 以及 MVC与依赖注入的原理
程序员文章站
2022-03-06 13:39:21
...
一. 多文件上传并将其封装成可复用的函数或者类:
1.逐个上传样式代码:
<?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>
bank.php函数库样式代码:
<?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;
}
多文件逐个上传效果预览:
2.多文件上传允许同时选择多个样式代码:
<?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>
效果预览:
二. MVC与依赖注入的原理,以及服务容器对依赖对象的管理实现过程
先创建模型model.php,再建立视图view.php,最后运行容器controller1.php
model.php样式代码:
<?php
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 $b) {
die('连接失败:' . $b->getMessage());
}
$sql = "SELECT id, name, gender,salary,email FROM staffs limit 4";
$stmt = $pdo->prepare($sql);
$stmt->execute();
return $stmt->fetchAll();
}
}
view.php样式代码:
<?php
namespace mvc_demo;
// 视图
class View
{
// 数据显示
public function fetch($datab)
{
$nob = '';
foreach ($datab as $data) {
$nob .= print_r($data, true) . '<br>';
}
return $nob;
}
}
controller1.php样式代码:
<?php
namespace 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);