Blogger Information
Blog 145
fans 7
comment 7
visits 164169
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP文件上传实战案例和MVC依赖注入和服务容器
李东亚¹⁸⁰³⁹⁵⁴⁰¹²⁰
Original
1156 people have browsed it

一、PHP文件上传实战

(一).文件上传知识相关点:

1.预定义变量:$_FILES-包含文件上传的所有信息(name,type,tmp_name,error,size)

  • name:上传文件的名字
  • type:上传文件的格式集
  • tmp_name:上传后的文件临时储存位置
  • error:上传文件的错误信息
  1. 0:文件上传成功
  2. 1:上传文件超过限制大小
  3. 2:上传文件超过表单限制大小
  4. 3:上传文件不完整
  5. 4:没有文件上传
  6. 5:未定义
  7. 6:找不到临时文件
  8. 7:写入失败,目录没有写入权限
  • size:文件的尺寸,用字节表示

2.函数:

  • move_uploaded_file('临时文件',"新文件"):将上传文件移动到新的位置;临时文件和新文件都有完整的路径和文件名字
  • is_uploaded_file("文件名字"):一般用来判断临时文件是否是由上传生成的;
  • strstr($str,'分割符');获取分隔符以后的所有字符(包含分隔符);如果由第三个参数为true时,获取分割符以前的所有字符(不包含分隔符);

(二)文件实战案例

1.代码
1.1处理上传文件

  1. <?php
  2. //定义上传文件类
  3. class UpLoad{
  4. // private $files=array();
  5. public function __construct($files){
  6. // $this->files=$files;
  7. $this->upload($files);
  8. }
  9. private function showerror(int $num):string
  10. {
  11. switch($num){
  12. case 1:
  13. $msg= "超过文件大小限制";
  14. break;
  15. case 2:
  16. $msg= "超过表单限制";
  17. break;
  18. case 3:
  19. $msg= "文件上传不完整";
  20. break;
  21. case 4:
  22. $msg= "没有文件上传";
  23. break;
  24. case 6:
  25. $msg= "找不到临时文件";
  26. break;
  27. case 7:
  28. $msg= "写入失败,目标目录没有权限";
  29. break;
  30. default:
  31. $msg= "未知错误";
  32. }
  33. return $msg;
  34. }
  35. private function upload($files){
  36. $files=$files["files"];
  37. // var_dump($files["error"]);
  38. if($files["error"]){
  39. foreach($files["error"] as $key=>$error){
  40. if($error===0){
  41. if(is_uploaded_file($files["tmp_name"][$key])){
  42. $ext=strstr($files["name"][$key],".");
  43. $filename=strstr($files["name"][$key],".",true);
  44. $newname=$filename.time().$ext;
  45. move_uploaded_file($files["tmp_name"][$key],$newname);
  46. echo "<img src='{$newname}' width=200;>";
  47. echo "<span>{$files["name"][$key]}上传成功</span>";
  48. }else{
  49. echo "非法操作";
  50. }
  51. }else{
  52. echo $this->showerror($error);
  53. }
  54. }
  55. }
  56. }
  57. }

1.2上传文件

  1. <?php
  2. //导入上传类
  3. include "upload.php";
  4. //调用上传类
  5. new UpLoad($_FILES);
  6. ?>
  7. <!DOCTYPE html>
  8. <html lang="en">
  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>文件上传</title>
  14. </head>
  15. <body>
  16. <form action="#" method="POST" enctype="multipart/form-data">
  17. <input name="files[]" type="file" multiple>
  18. <button>上传</button>
  19. </form>
  20. </body>
  21. </html>

2.运行效果


二 MVC服务容器管理依赖注入

(一)相关知识点

1.需要资源类:Closure;
2.call_user_func_array(函数名,参数);:函数名如果时对象中方法
可以[对象名,函数名];参数是数组;
3.服务容器类时,把需要的类是实列化后保存到一组类中关联数组中,使用时从类关联数组中拿出来使用;

(二)实战案例

1.M类

  1. <?php
  2. class Model{
  3. private $dsn="mysql:host=127.0.0.1;dbname=test;port=3306;charset=utf8mb4";
  4. private $username="admin";
  5. private $password="123456";
  6. private $pdo;
  7. public function __construct(){
  8. $this->pdo=new \PDO($this->dsn,$this->username,$this->password);
  9. }
  10. public function select($n,$m){
  11. $sql="select * from v_staffs limit ?,?";
  12. $stmt=$this->pdo->prepare($sql);
  13. $stmt->bindParam(1,$n,PDO::PARAM_INT);
  14. $stmt->bindParam(2,$m,PDO::PARAM_INT);
  15. $stmt->execute();
  16. // $stmt->debugDumpParams();
  17. return $res=$stmt->fetchAll(\PDO::FETCH_ASSOC);
  18. // var_dump($res);
  19. }
  20. }
  21. // echo (new Model())->select(0,5);

2.V类

  1. <?php
  2. // $index=include "index.php";
  3. class View
  4. {
  5. public function index($data){
  6. $html=<<<"EOT"
  7. <table border="1" cellpadding='3' cellspacing='0'>
  8. <caption>员工信息表</caption>
  9. <tr>
  10. <th>编号</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. EOT;
  20. $tr="";
  21. foreach($data as $item){
  22. // echo "<pre>".print_r($item,true);
  23. $tr.="<tr><td>".$item["id"]."</td><td>".$item["name"]."</td><td>".$item["age"]."</td><td>".$item["gender"]."</td><td>".$item["salary"]."</td><td>".$item["email"]."</td><td>".$item["postion"]."</td><td>".$item["area"]."</td></tr>";
  24. }
  25. echo $html.$tr."</table>";
  26. }
  27. }
  28. echo <<<EOT
  29. <style>
  30. table {
  31. width: 1000px;
  32. margin: 10px auto;
  33. }
  34. #page {
  35. margin-top: 10px;
  36. width: 1000px;
  37. display: flex;
  38. justify-content: space-around;
  39. }
  40. th,
  41. td {
  42. text-align: center;
  43. }
  44. #page>.active {
  45. background-color: green;
  46. color:white;
  47. }
  48. </style>
  49. EOT;

3.控制类

  1. <?php
  2. //容器
  3. // use Closure;
  4. class Container{
  5. protected $container=[];
  6. public function bind($name,Closure $class){
  7. $this->container[$name]=$class;
  8. }
  9. public function make($name,$params=[]){
  10. return call_user_func_array($this->container[$name],$params);
  11. }
  12. }
  13. class Controls{
  14. public function show(Container $name){
  15. // var_dump($name);
  16. // var_dump($name->make("Model"));
  17. $data=$name->make("Model")->select(0,10);
  18. // var_dump($data);
  19. $name->make("View")->index($data);
  20. }
  21. }
  22. include "Model.php";
  23. include "View.php";
  24. $container=new Container();
  25. $container->bind("Model",function(){return new Model();});
  26. $container->bind("View",function(){return new View();});
  27. (new Controls())->show($container);

4.运行效果

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