Blogger Information
Blog 45
fans 0
comment 0
visits 34830
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php实现多文件上传和mvc基础
咸鱼老爷
Original
674 people have browsed it

多文件上传

通过遍历$_FILES里面的数组实现多文件上传
把上传文件的代码封装称一个函数

  1. <?php
  2. function upload_error($error)
  3. {
  4. switch ($error) {
  5. case 1:
  6. $msg = '超过的单文件大小';
  7. break;
  8. case 2:
  9. $msg = '超过前端表单限制';
  10. break;
  11. case 3:
  12. $msg = '上传文件不完整';
  13. break;
  14. case 4:
  15. $msg = '没有文件被上传';
  16. break;
  17. case 6:
  18. $msg = '找不到临时目录';
  19. break;
  20. case 7:
  21. $msg = '写文失败,目录无写入权限';
  22. break;
  23. default:
  24. $msg = '未定义错误';
  25. }
  26. return $msg;
  27. }
  28. function upload($files){
  29. if(is_array($files)){
  30. foreach($files['error'] as $key=> $error){
  31. if($error === 0){
  32. $patch='uploads/'.md5(strstr($files['name'][$key],'.',true)).strstr($files['name'][$key],'.');
  33. if(move_uploaded_file($files['tmp_name'][$key],$patch)){
  34. echo "<p> {$files['name'][$key]}上传成功</p>";
  35. echo "<img src='{$patch}' width='150'>";
  36. }else{
  37. echo upload_error($error);
  38. }
  39. }
  40. }
  41. }
  42. }

前端页面

  1. <!-- 多文件批量上传 -->
  2. <?php
  3. $files=$_FILES['files'];
  4. include 'common.php';
  5. echo upload($files);
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="zh-CN">
  9. <head>
  10. <meta charset="UTF-8">
  11. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  12. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  13. <title>Document</title>
  14. </head>
  15. <body>
  16. <form action="" method="post" enctype="multipart/form-data">
  17. <fieldset>
  18. <legend>多文件上传</legend>
  19. <input type="file" name="files[]" multiple>
  20. <button>上传</button>
  21. </fieldset>
  22. </form>
  23. </body>
  24. </html>

效果图

mvc

  • M:model,模型,数据库的操作
  • v:view,视图,页面html
  • c:controller,控制器
    模型
    ```php
    <?php
    namespace mvc;

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);
}
}

  1. 视图
  2. ```php
  3. namespace mvc;
  4. class View{
  5. public function fetch($data){
  6. $table='<table>';
  7. $table.=' <caption>员工列表</caption>
  8. <thead>
  9. <tr>
  10. <th>id</th>
  11. <th>姓名</th>
  12. <th>年龄</th>
  13. <th>性别</th>
  14. <th>邮箱</th>
  15. <th>生日</th>
  16. <th>入职日期</th>
  17. <th>操作</th>
  18. </tr>
  19. <tbody>';
  20. foreach ($data as $v){
  21. $table.='<tr>';
  22. $table.='<td>'. $v['id'].'</td>';
  23. $table.='<td>'. $v['name'].'</td>';
  24. $table.='<td>'. $v['age'].'</td>';
  25. $table.='<td>'. $v['gender'].'</td>';
  26. $table.='<td>'. $v['email'].'</td>';
  27. $table.='<td>'. $v['borthday'].'</td>';
  28. $table.='<td>'. $v['create_time'].'</td>';
  29. $table.='<td><button>编辑</button> <button>删除</button></td>';
  30. $table.='</tr>';
  31. }
  32. $table.='</tbody>';
  33. return $table;
  34. }
  35. }

控制器

  1. namespace mvc;
  2. // 加载模型和视图
  3. require 'model.php';
  4. require 'view.php';
  5. class Controller1{
  6. public function index(){
  7. $md=new Model();
  8. $data=$md->getData();
  9. $view=new View();
  10. return $view->fetch($data);
  11. }
  12. }
  13. // 客户端测试
  14. // 创建控制器对象
  15. $controller=new Controller1();
  16. echo $controller->index();

效果图

依赖注入

解决对象之间的耦合

  1. <?php
  2. namespace mvc;
  3. // 加载模型和视图
  4. require 'model.php';
  5. require 'view.php';
  6. class Controller2{
  7. public function index($md,$view){
  8. $data=$md->getData();
  9. return $view->fetch($data);
  10. }
  11. }
  12. // 客户端测试
  13. // 创建控制器对象
  14. $controller=new Controller2();
  15. $md=new Model();
  16. $view=new View();
  17. echo $controller->index($md,$view);

使用构造函数进行依赖注入

  1. <?php
  2. namespace mvc;
  3. // 加载模型和视图
  4. require 'model.php';
  5. require 'view.php';
  6. class Controller3
  7. {
  8. private $model;
  9. private $view;
  10. public function __construct($model, $view)
  11. {
  12. $this->model = $model;
  13. $this->view = $view;
  14. }
  15. public function index()
  16. {
  17. $data = $this->model -> getData();
  18. return $this->view->fetch($data);
  19. }
  20. }
  21. // 客户端测试
  22. // 创建控制器对象
  23. $md = new Model();
  24. $view = new View();
  25. $controller = new Controller3($md, $view);
  26. echo $controller->index();

容器

将外部依赖对象进行统一管理

  1. namespace mvc;
  2. use Closure;
  3. // 加载模型和视图
  4. require 'model.php';
  5. require 'view.php';
  6. // 服务容器
  7. class Container{
  8. //对象容器
  9. protected $instances=[];
  10. // 添加对象
  11. public function bind($alias,Closure $process){
  12. $this->instances[$alias]=$process;
  13. }
  14. // 取出对象
  15. public function make($alias,$params=[]){
  16. return call_user_func_array($this->instances[$alias],[]);
  17. }
  18. }
  19. // 将依赖的外部对象添加到容器中
  20. $container=new Container();
  21. $container->bind('model',function(){return new Model();});
  22. $container->bind('view',function(){return new View();});
  23. class Controller4
  24. {
  25. public function index(Container $container)
  26. {
  27. $data = $container->make('model')-> getData();
  28. return $container->make('view')->fetch($data);
  29. }
  30. }
  31. // 客户端测试
  32. $controller = new Controller4();
  33. echo $controller->index($container);

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post