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

搜索
博主信息
博文 17
粉絲 1
評(píng)論 0
訪問(wèn)量 17746
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
PHP多文件上傳案和MVC簡(jiǎn)單案例以及MVC的依賴注入和服務(wù)容器的原理和實(shí)現(xiàn)過(guò)程
zl的php學(xué)習(xí)博客
原創(chuàng)
846人瀏覽過(guò)
  1. 寫一個(gè)實(shí)現(xiàn)的多文件上傳案例,并將它封裝成一個(gè)可復(fù)用的函數(shù)或類 2. 實(shí)例演示MVC與依賴注入的原理,以及服務(wù)容器對(duì)依賴對(duì)象的管理的實(shí)現(xiàn)過(guò)程。。。。

1. 多文件上傳案例

文件夾結(jié)構(gòu)

  • upload.php
  • common.php
  • uploads

uplaod.php

  1. <?php
  2. printf('<pre>%s</pre>', print_r($_FILES, true));
  3. if (count($_FILES) > 0) {
  4. include './common.php';
  5. switch ($_GET['t']) {
  6. case 'alone':
  7. echo upload($_FILES['file']);
  8. break;
  9. case 'onebyone':
  10. foreach ($_FILES as $file) {
  11. // echo $file['name'];
  12. echo upload($file);
  13. }
  14. break;
  15. case 'multipart':
  16. $files = [];
  17. foreach ($_FILES['file']['error'] as $key => $value) {
  18. $files[] = [
  19. 'name' => $_FILES['file']['name'][$key],
  20. 'type' => $_FILES['file']['type'][$key],
  21. 'tmp_name' => $_FILES['file']['tmp_name'][$key],
  22. 'error' => $_FILES['file']['error'][$key],
  23. 'size' => $_FILES['file']['size'][$key],
  24. ];
  25. }
  26. foreach ($files as $file) {
  27. // echo $file['name'];
  28. echo upload($file);
  29. }
  30. // var_dump($files);
  31. break;
  32. default:
  33. echo '未知錯(cuò)誤';
  34. }
  35. }
  36. ?>
  37. <!DOCTYPE html>
  38. <html lang="en">
  39. <head>
  40. <meta charset="UTF-8">
  41. <title>圖片上傳</title>
  42. <style>
  43. form {
  44. width: 20em;
  45. margin: 1em auto;
  46. }
  47. fieldset {
  48. margin: 1em;
  49. display: grid;
  50. place-content: flex-start;
  51. }
  52. button[type=button] {
  53. margin-bottom: .5em;
  54. }
  55. </style>
  56. </head>
  57. <body>
  58. <form action="upload.php?t=alone" method="post" enctype="multipart/form-data">
  59. <fieldset>
  60. <label>單文件上傳</label>
  61. <input type="hidden" name="MAX_FILE_SIZE" value="10485760">
  62. <input type="file" name="file">
  63. <button>upload</button>
  64. </fieldset>
  65. </form>
  66. <form action="upload.php?t=onebyone" method="post" enctype="multipart/form-data">
  67. <fieldset>
  68. <label>多文件上傳: 逐個(gè)上傳</label>
  69. <input type="hidden" name="MAX_FILE_SIZE" value="10485760">
  70. <input type="file" name="file1">
  71. <button type="button" onclick="oneByOne()">繼續(xù)添加文件</button>
  72. <button>upload</button>
  73. </fieldset>
  74. </form>
  75. <form action="upload.php?t=multipart" method="post" enctype="multipart/form-data">
  76. <fieldset>
  77. <label>多文件上傳: 一次性上傳</label>
  78. <input type="hidden" name="MAX_FILE_SIZE" value="10485760">
  79. <input type="file" name="file[]" multiple>
  80. <button>upload</button>
  81. </fieldset>
  82. </form>
  83. <script>
  84. let oneByOneFileNameIndex = 1;
  85. // 動(dòng)態(tài)添加逐個(gè)上傳的input
  86. function oneByOne() {
  87. const dom = document.querySelector(`input[name=file${oneByOneFileNameIndex}]`);
  88. const input = document.createElement('input');
  89. input.type = 'file'
  90. input.name = 'file' + ++oneByOneFileNameIndex;
  91. dom.insertAdjacentElement('afterend', input);
  92. }
  93. </script>
  94. </body>
  95. </html>

common.php

  1. <?php
  2. /**
  3. * 文件上傳
  4. *
  5. * @param array $files: 一個(gè)文件的所有數(shù)據(jù)
  6. * @return string
  7. */
  8. function upload(array $files): string
  9. {
  10. // 獲取文件名
  11. $name = $files['name'];
  12. // 獲取臨時(shí)文件的路徑
  13. $tmpName = $files['tmp_name'];
  14. // 獲取錯(cuò)誤信息
  15. $error = $files['error'];
  16. if ($error === 0) {
  17. // 獲取文件類型
  18. $type = strstr($files['type'], '/', true);
  19. // 獲取文件后綴
  20. $ext = '.' . strtolower(pathinfo($files['name'], PATHINFO_EXTENSION));
  21. if ($type === 'image') {
  22. $path = "uploads/" . md5($name . date('Ymd') . str_pad(mt_rand(1, 9999), 5, '0', STR_PAD_LEFT)) . $ext;
  23. // 檢測(cè)臨時(shí)文件的合法性
  24. if (is_uploaded_file($tmpName)) {
  25. // 移動(dòng)文件
  26. if (move_uploaded_file($tmpName, $path)) {
  27. // 返回成功
  28. return "<span><p style=\"color:green\">{$name}上傳成功</p><img src='{$path}' width=250 alt='{$name}' /></span>";
  29. } else {
  30. return '移動(dòng)文件失敗';
  31. }
  32. } else {
  33. return '臨時(shí)文件不合法';
  34. }
  35. } else {
  36. return '上傳類型錯(cuò)誤';
  37. }
  38. } else {
  39. return upload_error($error);
  40. }
  41. }
  42. /**
  43. * 文件上傳的錯(cuò)誤信息集合
  44. *
  45. * @param int $errno: error代碼
  46. * @return string
  47. */
  48. function upload_error(int $errno): string
  49. {
  50. switch ($errno) {
  51. case 1:
  52. $msg = '超過(guò)的單文件大小';
  53. break;
  54. case 2:
  55. $msg = '超過(guò)前端表單限制';
  56. break;
  57. case 3:
  58. $msg = '上傳文件不完整';
  59. break;
  60. case 4:
  61. $msg = '沒(méi)有文件被上傳';
  62. break;
  63. case 6:
  64. $msg = '找不到臨時(shí)目錄';
  65. break;
  66. case 7:
  67. $msg = '寫入失敗,目標(biāo)目錄無(wú)寫入權(quán)限';
  68. break;
  69. default:
  70. exit('未定義錯(cuò)誤');
  71. }
  72. return $msg;
  73. }

2.

  1. MVC:
  2. M-模型(數(shù)據(jù)庫(kù)相關(guān)的操作),
  3. V-視圖(頁(yè)面,html),
  4. C-控制器(功能:1.獲取數(shù)據(jù) 2.選擇視圖)
  5. 依賴注入: 簡(jiǎn)單那來(lái)說(shuō)就是為了解決對(duì)象之間的耦合(`在一個(gè)類中調(diào)用其他類的實(shí)例`),例2.1:
  6. 方法: 在類的外部實(shí)例化其他類, 然后以參數(shù)的方式傳入.例:2.2:
  7. 服務(wù)容器: 將外部依賴(需要引用的外部類實(shí)例)進(jìn)行統(tǒng)一的管理.例2.3

2.1

  1. <?php
  2. namespace test;
  3. require './Model.php';
  4. require './View.php';
  5. // 服務(wù)容器
  6. class Conn
  7. // 控制器
  8. class Controller
  9. {
  10. public function index(): string
  11. {
  12. return (new View())->fetch((new \test\Model())->select()['data']);
  13. }
  14. }

2.2

  1. <?php
  2. namespace test;
  3. require './Model.php';
  4. require './View.php';
  5. // 控制器
  6. class Controller
  7. {
  8. // public function index(): string
  9. // {
  10. // return (new View())->fetch((new \test\Model())->select()['data']);
  11. // }
  12. protected $model;
  13. protected $view;
  14. public function __construct($model,$view) {
  15. $this->model = $model;
  16. $this->view = $view;
  17. }
  18. public function index(): string
  19. {
  20. return $this->view->fetch($this->model->select()['data']);
  21. }
  22. }
  23. $model = new Model();
  24. $view = new View();
  25. $controller = new Controller($model, $view);
  26. echo $controller->index();

2.3

  1. <?php
  2. namespace test;
  3. require './Model.php';
  4. require './View.php';
  5. // 服務(wù)容器
  6. class Container
  7. {
  8. // 對(duì)象容器
  9. protected $instanles = [];
  10. // 寫入對(duì)象
  11. public function bind($alias, \Closure $process)
  12. {
  13. $this->instanles[$alias] = $process;;
  14. }
  15. // 取出對(duì)象
  16. public function make($alias, $params = [])
  17. {
  18. return call_user_func_array($this->instanles[$alias], $params);
  19. }
  20. }
  21. // 控制器
  22. class Controller
  23. {
  24. /*服務(wù)容器*/
  25. public function index(Container $instanles)
  26. {
  27. return $instanles->make('view')->fetch($instanles->make('model')->select()['data']);
  28. }
  29. }

3. MVC簡(jiǎn)單案例(數(shù)據(jù)來(lái)源: http://ipnx.cn/blog/detail/26509.html)

文件夾結(jié)構(gòu)

  • Model.php
  • View.php
  • Controller.php
  • index.php

Model.php

  1. <?php
  2. namespace mvc_demo;
  3. use PDO;
  4. // 模型類
  5. class Model
  6. {
  7. // 獲取數(shù)據(jù)
  8. public function getData()
  9. {
  10. $pdo = new PDO('mysql:dbname=phpedu', 'root', 'root');
  11. $stmt = $pdo->prepare('select * from staffs limit 10');
  12. $stmt->execute();
  13. return $stmt->fetchAll(PDO::FETCH_ASSOC);
  14. }
  15. }

View.php

  1. <?php
  2. namespace mvc_demo;
  3. // 視圖
  4. class View
  5. {
  6. // 數(shù)據(jù)展示
  7. public function fetch($data)
  8. {
  9. $table = '<table border="1" cellspacing="0">';
  10. $table .= '<caption>員工信息表</caption>
  11. <tr bgcolor="lightcyan">
  12. <th>id</th>
  13. <td>姓名</td>
  14. <td>性別</td>
  15. <td>工資</td>
  16. <td>郵箱</td>
  17. <td>生日</td>
  18. </tr>';
  19. foreach ($data as $staff) {
  20. $table .= '<tr>';
  21. $table .= '<td>' . $staff['sid'] . '</td>';
  22. $table .= '<td>' . $staff['name'] . '</td>';
  23. $table .= '<td>' . $staff['gender'] . '</td>';
  24. $table .= '<td>' . $staff['salary'] . '</td>';
  25. $table .= '<td>' . $staff['email'] . '</td>';
  26. $table .= '<td>' . $staff['birthday'] . '</td>';
  27. $table .= '</tr>';
  28. }
  29. $table .= '</table>';
  30. return $table;
  31. }
  32. }

Controller.php

  1. <?php
  2. namespace test;
  3. require './Model.php';
  4. require './View.php';
  5. // 服務(wù)容器
  6. class Container
  7. {
  8. // 對(duì)象容器
  9. protected $instanles = [];
  10. // 寫入對(duì)象
  11. public function bind($alias, \Closure $process)
  12. {
  13. $this->instanles[$alias] = $process;;
  14. }
  15. // 取出對(duì)象
  16. public function make($alias, $params = [])
  17. {
  18. return call_user_func_array($this->instanles[$alias], $params);
  19. }
  20. }
  21. // 控制器
  22. class Controller
  23. {
  24. // public function index(): string
  25. // {
  26. // return (new View())->fetch((new \test\Model())->select()['data']);
  27. // }
  28. /*依賴注入*/
  29. // protected $model;
  30. // protected $view;
  31. //
  32. // public function __construct($model, $view)
  33. // {
  34. // $this->model = $model;
  35. // $this->view = $view;
  36. // }
  37. //
  38. // public function index(): string
  39. // {
  40. // return $this->view->fetch($this->model->select()['data']);
  41. // }
  42. /*服務(wù)容器*/
  43. public function index(Container $instanles)
  44. {
  45. return $instanles->make('view')->fetch($instanles->make('model')->select()['data']);
  46. }
  47. }

index.php

  1. <?php
  2. /*服務(wù)容器*/
  3. require './Controller.php';
  4. $container = new \test\Container();
  5. $container->bind('model', function (){return new \test\Model();});
  6. $container->bind('view', function (){return new \test\View();});
  7. $table = new \test\Controller();
  8. ?>
  9. <!DOCTYPE html>
  10. <html lang="en">
  11. <head>
  12. <meta charset="utf-8">
  13. <title>mvc</title>
  14. </head>
  15. <body>
  16. <?php echo $table->index($container);?>
  17. </body>
  18. </html>
批改老師:天蓬老師天蓬老師

批改狀態(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é)申明 意見反饋 講師合作 廣告合作 最新更新
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é)