
批改狀態(tài):合格
老師批語(yǔ):
1.預(yù)定義變量:$_FILES
-包含文件上傳的所有信息(name,type,tmp_name,error,size)
0:文件上傳成功
1:上傳文件超過(guò)限制大小
2:上傳文件超過(guò)表單限制大小
3:上傳文件不完整
4:沒(méi)有文件上傳
5:未定義
6:找不到臨時(shí)文件
7:寫(xiě)入失敗,目錄沒(méi)有寫(xiě)入權(quán)限
2.函數(shù):
move_uploaded_file('臨時(shí)文件',"新文件")
:將上傳文件移動(dòng)到新的位置;臨時(shí)文件和新文件都有完整的路徑和文件名字is_uploaded_file("文件名字")
:一般用來(lái)判斷臨時(shí)文件是否是由上傳生成的;strstr($str,'分割符');
獲取分隔符以后的所有字符(包含分隔符);如果由第三個(gè)參數(shù)為true時(shí),獲取分割符以前的所有字符(不包含分隔符);1.代碼
1.1處理上傳文件
<?php
//定義上傳文件類(lèi)
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= "超過(guò)文件大小限制";
break;
case 2:
$msg= "超過(guò)表單限制";
break;
case 3:
$msg= "文件上傳不完整";
break;
case 4:
$msg= "沒(méi)有文件上傳";
break;
case 6:
$msg= "找不到臨時(shí)文件";
break;
case 7:
$msg= "寫(xiě)入失敗,目標(biāo)目錄沒(méi)有權(quán)限";
break;
default:
$msg= "未知錯(cuò)誤";
}
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
//導(dǎo)入上傳類(lèi)
include "upload.php";
//調(diào)用上傳類(lèi)
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.運(yùn)行效果
1.需要資源類(lèi):Closure
;
2.call_user_func_array(函數(shù)名,參數(shù));
:函數(shù)名如果時(shí)對(duì)象中方法
可以[對(duì)象名,函數(shù)名]
;參數(shù)是數(shù)組;
3.服務(wù)容器類(lèi)時(shí),把需要的類(lèi)是實(shí)列化后保存到一組類(lèi)中關(guān)聯(lián)數(shù)組中,使用時(shí)從類(lèi)關(guān)聯(lián)數(shù)組中拿出來(lái)使用;
1.M類(lèi)
<?php
class 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類(lèi)
<?php
// $index=include "index.php";
class View
{
public function index($data){
$html=<<<"EOT"
<table border="1" cellpadding='3' cellspacing='0'>
<caption>員工信息表</caption>
<tr>
<th>編號(hào)</th>
<th>姓名</th>
<th>年齡</th>
<th>性別</th>
<th>工資</th>
<th>郵箱</th>
<th>職位</th>
<th>負(fù)責(zé)區(qū)域</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.控制類(lèi)
<?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.運(yùn)行效果
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號(hào)
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)