批改状态:合格
老师批语:
1.预定义变量:$_FILES-包含文件上传的所有信息(name,type,tmp_name,error,size)
0:文件上传成功1:上传文件超过限制大小2:上传文件超过表单限制大小3:上传文件不完整4:没有文件上传5:未定义6:找不到临时文件7:写入失败,目录没有写入权限
2.函数:
move_uploaded_file('临时文件',"新文件"):将上传文件移动到新的位置;临时文件和新文件都有完整的路径和文件名字is_uploaded_file("文件名字"):一般用来判断临时文件是否是由上传生成的;strstr($str,'分割符');获取分隔符以后的所有字符(包含分隔符);如果由第三个参数为true时,获取分割符以前的所有字符(不包含分隔符);1.代码
1.1处理上传文件
<?php//定义上传文件类class UpLoad{// private $files=array();public function __construct($files){// $this->files=$files;$this->upload($files);}private function showerror(int $num):string{switch($num){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:$msg= "未知错误";}return $msg;}private function upload($files){$files=$files["files"];// var_dump($files["error"]);if($files["error"]){foreach($files["error"] as $key=>$error){if($error===0){if(is_uploaded_file($files["tmp_name"][$key])){$ext=strstr($files["name"][$key],".");$filename=strstr($files["name"][$key],".",true);$newname=$filename.time().$ext;move_uploaded_file($files["tmp_name"][$key],$newname);echo "<img src='{$newname}' width=200;>";echo "<span>{$files["name"][$key]}上传成功</span>";}else{echo "非法操作";}}else{echo $this->showerror($error);}}}}}
1.2上传文件
<?php//导入上传类include "upload.php";//调用上传类new UpLoad($_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"><input name="files[]" type="file" multiple><button>上传</button></form></body></html>
2.运行效果
1.需要资源类:Closure;
2.call_user_func_array(函数名,参数);:函数名如果时对象中方法
可以[对象名,函数名];参数是数组;
3.服务容器类时,把需要的类是实列化后保存到一组类中关联数组中,使用时从类关联数组中拿出来使用;
1.M类
<?phpclass Model{private $dsn="mysql:host=127.0.0.1;dbname=test;port=3306;charset=utf8mb4";private $username="admin";private $password="123456";private $pdo;public function __construct(){$this->pdo=new \PDO($this->dsn,$this->username,$this->password);}public function select($n,$m){$sql="select * from v_staffs limit ?,?";$stmt=$this->pdo->prepare($sql);$stmt->bindParam(1,$n,PDO::PARAM_INT);$stmt->bindParam(2,$m,PDO::PARAM_INT);$stmt->execute();// $stmt->debugDumpParams();return $res=$stmt->fetchAll(\PDO::FETCH_ASSOC);// var_dump($res);}}// echo (new Model())->select(0,5);
2.V类
<?php// $index=include "index.php";class View{public function index($data){$html=<<<"EOT"<table border="1" cellpadding='3' cellspacing='0'><caption>员工信息表</caption><tr><th>编号</th><th>姓名</th><th>年龄</th><th>性别</th><th>工资</th><th>邮箱</th><th>职位</th><th>负责区域</th></tr>EOT;$tr="";foreach($data as $item){// echo "<pre>".print_r($item,true);$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>";}echo $html.$tr."</table>";}}echo <<<EOT<style>table {width: 1000px;margin: 10px auto;}#page {margin-top: 10px;width: 1000px;display: flex;justify-content: space-around;}th,td {text-align: center;}#page>.active {background-color: green;color:white;}</style>EOT;
3.控制类
<?php//容器// use Closure;class Container{protected $container=[];public function bind($name,Closure $class){$this->container[$name]=$class;}public function make($name,$params=[]){return call_user_func_array($this->container[$name],$params);}}class Controls{public function show(Container $name){// var_dump($name);// var_dump($name->make("Model"));$data=$name->make("Model")->select(0,10);// var_dump($data);$name->make("View")->index($data);}}include "Model.php";include "View.php";$container=new Container();$container->bind("Model",function(){return new Model();});$container->bind("View",function(){return new View();});(new Controls())->show($container);
4.运行效果
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号