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

搜索
博主信息
博文 64
粉絲 6
評(píng)論 2
訪問量 100650
最新下載
更多>
網(wǎng)站特效
網(wǎng)站源碼
網(wǎng)站素材
前端模板
PHP中的文件上傳
王嬌
原創(chuàng)
977人瀏覽過

學(xué)習(xí)總結(jié)

  • 文件上傳操作封裝在類中利于使用和維護(hù)
  • 文件批量上傳時(shí)前端的input中寫multiple參數(shù)
  • 文件上傳時(shí)前端form必須是POST方式上傳,method="POST" enctype="multipart/form-data",而且表單發(fā)送前的編碼方式必須是二進(jìn)制編碼。

1.單文件上傳類 upFException.php

  1. <?php
  2. namespace compotents\conn
  3. {
  4. include 'upFException.php';
  5. class UploadFile
  6. {
  7. private $oriFileName;//原始文件名
  8. private $fileType;//文件類型
  9. private $error;//錯(cuò)誤類型
  10. private $tmpFileName;//臨時(shí)文件名
  11. private $fileSize;//已上傳文件大小
  12. public function __construct($file)
  13. {
  14. $this->oriFileName = $file['name'];
  15. $this->fileType = $file['type'];
  16. $this->error = $file['error'];
  17. $this->tmpFileName = $file['tmp_name'];
  18. $this->fileSize = $file['size'];
  19. }
  20. public function checkSuccess($fileType):bool//檢測文件是否上傳成功
  21. {
  22. $error = $this->error;
  23. $type = strstr($this->fileType,'/',true);
  24. try {
  25. if ($error > UPLOAD_ERR_OK) :
  26. switch ($error) :
  27. case UPLOAD_ERR_INI_SIZE:
  28. throw new upFException('上傳的文件超過了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值', 1);
  29. break;
  30. case UPLOAD_ERR_FORM_SIZE:
  31. throw new upFException('上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值', 2);
  32. break;
  33. case UPLOAD_ERR_PARTIAL:
  34. throw new upFException('文件只有部分被上傳', 3);
  35. break;
  36. case UPLOAD_ERR_NO_FILE:
  37. throw new upFException('沒有文件被上傳', 4);
  38. break;
  39. case UPLOAD_ERR_NO_TMP_DIR:
  40. throw new upFException('找不到臨時(shí)文件夾', 6);
  41. break;
  42. case UPLOAD_ERR_CANT_WRITE:
  43. throw new upFException('文件寫入失敗', 7);
  44. break;
  45. default:
  46. throw new upFException('未知類型錯(cuò)誤', 8);
  47. endswitch;
  48. endif;
  49. if ($type !== strtolower($fileType)):
  50. throw new upFException('文件類型錯(cuò)誤', 9);
  51. endif;
  52. return true;
  53. }
  54. catch (upFException $e)
  55. {
  56. echo $e;
  57. return false;
  58. }
  59. }
  60. public function saveFile($destDir,$destFileName)
  61. {
  62. $tmp = $this->tmpFileName;
  63. $ori = $this->oriFileName;
  64. $dest = $destDir.$destFileName;
  65. if(is_uploaded_file($tmp))://判斷是否是post方式上傳的文件
  66. move_uploaded_file($tmp,$dest);
  67. endif;
  68. }
  69. }
  70. }
  71. ?>
  • 單文件上傳前端處理部分代碼
  1. case 'regist':
  2. if($_SERVER['REQUEST_METHOD']==='POST'):
  3. $name = trim($_POST['userName']);
  4. $nc = trim($_POST['userNc']);
  5. $pwd = md5(trim($_POST['pwd1']));
  6. $tpwd = trim($_POST['pwd1']);
  7. $rdate = date('Y-m-d');
  8. //使用用戶名生成文件頭像
  9. $fileName = strstr($name,'.',true).'.png';
  10. //生成一個(gè)文件上傳的對(duì)象
  11. $file = new UploadFile($_FILES['userHeadImg']);
  12. //檢測文件是否上傳成功,參數(shù)是文件上傳的類型限定
  13. if($file->checkSuccess('image')):
  14. //如果文件上傳成功,則保存文件到服務(wù)器上
  15. $destDir = $_SERVER['DOCUMENT_ROOT']. '/php11/0511/images/headImg/';
  16. $file->saveFile($destDir,$fileName);
  17. else:
  18. exit('<script>alert("頭像上傳失敗");location.href="register.php";</script>');
  19. endif;
  20. $data = ['name'=>"$name",'nc'=>"$nc",'password'=>"$pwd",'tpassword'=>"$tpwd",'regdate'=>"$rdate",'headimg'=>"$fileName"];
  21. //先判斷一下用戶名是否已經(jīng)注冊(cè)
  22. $where = "`name`='$name'";
  23. $res = $user->select($table,$where);
  24. if(count($res)):
  25. exit('<script>alert("郵箱已經(jīng)注冊(cè)過了,可直接登錄");location.href="login.php";</script>');
  26. else:
  27. $rowCount = $user->insert($table,$data); //返回受影響的記錄條數(shù)
  28. if($rowCount):
  29. exit('<script>alert("注冊(cè)成功");location.href="login.php";</script>');
  30. else:
  31. exit('<script>alert("注冊(cè)失敗");location.href="register.php";</script>');
  32. endif;
  33. endif;
  34. else:
  35. die('請(qǐng)求類型錯(cuò)誤!');
  36. endif;
  • 實(shí)現(xiàn)效果

2.文件批量上傳類 UploadMulFile.php

  1. <?php
  2. namespace compotents\conn
  3. {
  4. include 'upFException.php';
  5. class UploadMulFile
  6. {
  7. private $oriFileName = [];//原始文件名
  8. private $fileType = [];//文件類型
  9. private $error = [];//錯(cuò)誤類型
  10. private $tmpFileName = [];//臨時(shí)文件名
  11. private $fileSize = [];//已上傳文件大小
  12. public function __construct($file)
  13. {
  14. $this->oriFileName = $file['name'];
  15. $this->fileType = $file['type'];
  16. $this->error = $file['error'];
  17. $this->tmpFileName = $file['tmp_name'];
  18. $this->fileSize = $file['size'];
  19. }
  20. public function checkSuccess($fileType):bool//檢測文件是否上傳成功
  21. {
  22. $errors = $this->error;
  23. foreach($errors as $key => $error ):
  24. $type = strstr($this->fileType[$key],'/',true);
  25. $ori = $this->oriFileName[$key];
  26. try {
  27. if ($error > UPLOAD_ERR_OK) :
  28. switch ($error) :
  29. case UPLOAD_ERR_INI_SIZE:
  30. throw new upFException($ori.'上傳的文件超過了 php.ini 中 upload_max_filesize 選項(xiàng)限制的值', 1);
  31. break;
  32. case UPLOAD_ERR_FORM_SIZE:
  33. throw new upFException($ori.'上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項(xiàng)指定的值', 2);
  34. break;
  35. case UPLOAD_ERR_PARTIAL:
  36. throw new upFException($ori.'文件只有部分被上傳', 3);
  37. break;
  38. case UPLOAD_ERR_NO_FILE:
  39. throw new upFException($ori.'沒有文件被上傳', 4);
  40. break;
  41. case UPLOAD_ERR_NO_TMP_DIR:
  42. throw new upFException($ori.'找不到臨時(shí)文件夾', 6);
  43. break;
  44. case UPLOAD_ERR_CANT_WRITE:
  45. throw new upFException($ori.'文件寫入失敗', 7);
  46. break;
  47. default:
  48. throw new upFException($ori.'未知類型錯(cuò)誤', 8);
  49. endswitch;
  50. endif;
  51. if ($type !== strtolower($fileType)):
  52. throw new upFException($ori.'文件類型錯(cuò)誤', 9);
  53. endif;
  54. }
  55. catch (upFException $e)
  56. {
  57. echo $e;
  58. return false;
  59. }
  60. endforeach;
  61. return true;//如果沒有錯(cuò)誤,返回true
  62. }
  63. public function saveFile($destDir):array
  64. {
  65. $tmps = $this->tmpFileName;
  66. $images = [];
  67. foreach($tmps as $key => $tmp):
  68. $tmp = $this->tmpFileName[$key];
  69. $ori = $this->oriFileName[$key];
  70. $images[$key] = $ori;
  71. $dest = $destDir.$ori;
  72. if(is_uploaded_file($tmp))://判斷是否是post方式上傳的文件
  73. move_uploaded_file($tmp,$dest);
  74. endif;
  75. endforeach;
  76. return $images;
  77. }
  78. }
  79. }
  80. ?>
  • 文件批量上傳前端代碼 showGoods.php
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <link rel="stylesheet" href="css/form.css">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <div class="showgoods">
  11. <?php
  12. require 'autoLoad.php';
  13. use compotents\conn\UploadMulFile;
  14. if(count($_FILES)):
  15. $file = new UploadMulFile($_FILES['goods']);
  16. if($file->checkSuccess('image')):
  17. //如果文件上傳成功,則保存文件到服務(wù)器上
  18. $destDir = $_SERVER['DOCUMENT_ROOT']. '/php11/0511/images/goods/';
  19. $images = $file->saveFile($destDir,$fileName);
  20. endif;
  21. ?>
  22. <div style="display: flex;flex-flow: row wrap;justify-content: space-evenly;
  23. align-items: center;">
  24. <?php foreach($images as $image):?>
  25. <div
  26. style="width: 180px;height:150px; display:flex;justify-content:cetner;align-items: center;margin: 10px 10px;">
  27. <img src="images/goods/<?php echo $image ?>" alt=""></div>
  28. <?php endforeach; ?>
  29. </div>
  30. <?php
  31. else:
  32. ?>
  33. <form action="" method="POST" enctype="multipart/form-data">
  34. <div>
  35. <input type="file" name="goods[]" multiple>
  36. </div>
  37. <div><button type="submit">上傳</button></div>
  38. </form>
  39. <?php
  40. endif;
  41. ?>
  42. </div>
  43. </body>
  44. </html>
  • 代碼效果圖

3.文件上傳異常類 upFException.php

  1. <?
  2. namespace compotents\conn
  3. {
  4. use Exception;
  5. class upFException extends Exception
  6. {
  7. // 在異常子類中,可以訪問并重寫Exception中的四個(gè)屬性,通過__toString()格式化異常輸出信息
  8. public function __toString()
  9. {
  10. return <<< UPLOAD
  11. <style>
  12. table {border-collapse: collapse;border:1px solid black;text-align: center;}
  13. td {border:1px solid black;padding: 5px;}
  14. tr:first-of-type {background-color:#eee;
  15. }
  16. tr:last-of-type td {color: coral;}
  17. </style>
  18. <table>
  19. <tr><td>代碼</td><td>信息</td><td>文件</td><td>行號(hào)</td></tr>
  20. <tr><td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td></tr>
  21. </table>
  22. UPLOAD;
  23. }
  24. }
  25. }
  26. ?>
批改老師:天蓬老師天蓬老師

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

老師批語:給你個(gè)思考題, 想一下如何顯示上傳進(jìn)度
本博文版權(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í)者快速成長!
關(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é)