亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

搜索
博主信息
博文 145
粉絲 7
評(píng)論 7
訪問(wèn)量 198692
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
PHP文件上傳實(shí)戰(zhàn)案例和MVC依賴注入和服務(wù)容器
李東亞1??3????12?
原創(chuàng)
1413人瀏覽過(guò)

一、PHP文件上傳實(shí)戰(zhàn)

(一).文件上傳知識(shí)相關(guān)點(diǎn):

1.預(yù)定義變量:$_FILES-包含文件上傳的所有信息(name,type,tmp_name,error,size)

  • name:上傳文件的名字
  • type:上傳文件的格式集
  • tmp_name:上傳后的文件臨時(shí)儲(chǔ)存位置
  • error:上傳文件的錯(cuò)誤信息
  1. 0:文件上傳成功
  2. 1:上傳文件超過(guò)限制大小
  3. 2:上傳文件超過(guò)表單限制大小
  4. 3:上傳文件不完整
  5. 4:沒(méi)有文件上傳
  6. 5:未定義
  7. 6:找不到臨時(shí)文件
  8. 7:寫(xiě)入失敗,目錄沒(méi)有寫(xiě)入權(quán)限
  • size:文件的尺寸,用字節(jié)表示

2.函數(shù):

  • move_uploaded_file('臨時(shí)文件',"新文件"):將上傳文件移動(dòng)到新的位置;臨時(shí)文件和新文件都有完整的路徑和文件名字
  • is_uploaded_file("文件名字"):一般用來(lái)判斷臨時(shí)文件是否是由上傳生成的;
  • strstr($str,'分割符');獲取分隔符以后的所有字符(包含分隔符);如果由第三個(gè)參數(shù)為true時(shí),獲取分割符以前的所有字符(不包含分隔符);

(二)文件實(shí)戰(zhàn)案例

1.代碼
1.1處理上傳文件

  1. <?php
  2. //定義上傳文件類(lèi)
  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= "超過(guò)文件大小限制";
  14. break;
  15. case 2:
  16. $msg= "超過(guò)表單限制";
  17. break;
  18. case 3:
  19. $msg= "文件上傳不完整";
  20. break;
  21. case 4:
  22. $msg= "沒(méi)有文件上傳";
  23. break;
  24. case 6:
  25. $msg= "找不到臨時(shí)文件";
  26. break;
  27. case 7:
  28. $msg= "寫(xiě)入失敗,目標(biāo)目錄沒(méi)有權(quán)限";
  29. break;
  30. default:
  31. $msg= "未知錯(cuò)誤";
  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. //導(dǎo)入上傳類(lèi)
  3. include "upload.php";
  4. //調(diào)用上傳類(lèi)
  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.運(yùn)行效果


二 MVC服務(wù)容器管理依賴注入

(一)相關(guān)知識(shí)點(diǎ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)使用;

(二)實(shí)戰(zhàn)案例

1.M類(lèi)

  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類(lèi)

  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>編號(hào)</th>
  11. <th>姓名</th>
  12. <th>年齡</th>
  13. <th>性別</th>
  14. <th>工資</th>
  15. <th>郵箱</th>
  16. <th>職位</th>
  17. <th>負(fù)責(zé)區(qū)域</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.控制類(lèi)

  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.運(yùn)行效果

批改老師:天蓬老師天蓬老師

批改狀態(tài):合格

老師批語(yǔ):
本博文版權(quán)歸博主所有,轉(zhuǎn)載請(qǐng)注明地址!如有侵權(quán)、違法,請(qǐng)聯(lián)系admin@php.cn舉報(bào)處理!
全部評(píng)論 文明上網(wǎng)理性發(fā)言,請(qǐng)遵守新聞評(píng)論服務(wù)協(xié)議
0條評(píng)論
關(guān)于我們 免責(zé)申明 意見(jiàn)反饋 講師合作 廣告合作 最新更新
php中文網(wǎng):公益在線php培訓(xùn),幫助PHP學(xué)習(xí)者快速成長(zhǎng)!
關(guān)注服務(wù)號(hào) 技術(shù)交流群
PHP中文網(wǎng)訂閱號(hào)
每天精選資源文章推送
PHP中文網(wǎng)APP
隨時(shí)隨地碎片化學(xué)習(xí)
PHP中文網(wǎng)抖音號(hào)
發(fā)現(xiàn)有趣的

Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號(hào)

  • 登錄PHP中文網(wǎng),和優(yōu)秀的人一起學(xué)習(xí)!
    全站2000+教程免費(fèi)學(xué)