Blogger Information
Blog 52
fans 0
comment 3
visits 42664
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php学习:第19章 细说文件上传原理与实战(单文件、多个单文件、批量文件)
王小飞
Original
772 people have browsed it

1.单文件上传

代码

  1. <?php
  2. // $_FILES
  3. printf('<pre>%s</pre>', print_r($_FILES, true));
  4. // 自定义上传异常类
  5. class UploadException extends Exception
  6. {
  7. // 在异常子类中,可以访问并重写Exception中的四个属性,通过__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. tr:last-of-type td {color: coral;}
  16. </style>
  17. <table>
  18. <tr><td>代码</td><td>信息</td><td>文件</td><td>行号</td></tr>
  19. <tr><td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td></tr>
  20. </table>
  21. UPLOAD;
  22. }
  23. }
  24. ///////////////////////////////////////////////////////////////////////
  25. try {
  26. // 上传出错的代码 将出错代码保存到变量中
  27. $errorCode = $_FILES['my_pic']['error'];
  28. // 判断 出错代码是否大于0
  29. if ($errorCode > UPLOAD_ERR_OK) {
  30. //如果大于零 则便判断是几
  31. switch ($errorCode) {
  32. case UPLOAD_ERR_INI_SIZE:
  33. throw new UploadException('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值', 1);
  34. break;
  35. case UPLOAD_ERR_FORM_SIZE:
  36. throw new UploadException('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值', 2);
  37. break;
  38. case UPLOAD_ERR_PARTIAL:
  39. throw new UploadException('文件只有部分被上传', 3);
  40. break;
  41. case UPLOAD_ERR_NO_FILE:
  42. throw new UploadException('没有文件被上传', 4);
  43. break;
  44. case UPLOAD_ERR_NO_TMP_DIR:
  45. throw new UploadException('找不到临时文件夹', 6);
  46. break;
  47. case UPLOAD_ERR_CANT_WRITE:
  48. throw new UploadException('文件写入失败', 7);
  49. break;
  50. default:
  51. // 测试时建议关掉default: 避免误报影响
  52. throw new UploadException('未知类型错误', 8);
  53. }
  54. }
  55. // 判断文件类型
  56. $fileType = $_FILES['my_pic']['type'];
  57. if (strstr($fileType, '/', true) == ''){
  58. echo '请选择要上传的文件';
  59. }else{
  60. if (strstr($fileType, '/', true) !== 'image') throw new UploadException('文件类型错误',9);
  61. }
  62. }
  63. catch (UploadException $e) {
  64. echo $e;
  65. }
  66. ?>
  67. <!DOCTYPE html>
  68. <html lang="en">
  69. <head>
  70. <meta charset="UTF-8">
  71. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  72. <title>文件上传变量$_FILES</title>
  73. </head>
  74. <body>
  75. <hr>
  76. <form action="" method="POST" enctype="multipart/form-data">
  77. <fieldset>
  78. <legend>单文件上传: 异常处理</legend>
  79. <input type="hidden" name="MAX_FILE_SIZE" value="3000000">
  80. <input type="file" name="my_pic">
  81. <button>上传</button>
  82. </fieldset>
  83. </form>
  84. </body>
  85. </html>

效果:

上传效果:

错误提示:

2.多文件上传

代码

  1. <?php
  2. // $_FILES
  3. printf('<pre>%s</pre>', print_r($_FILES, true));
  4. // 自定义上传异常类
  5. class UploadException extends Exception
  6. {
  7. // 在异常子类中,可以访问并重写Exception中的四个属性,通过__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. tr:last-of-type td {color: coral;}
  16. </style>
  17. <table>
  18. <tr><td>代码</td><td>信息</td><td>文件</td><td>行号</td></tr>
  19. <tr><td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td></tr>
  20. </table>
  21. UPLOAD;
  22. }
  23. }
  24. try {
  25. // 上传出错的代码 将出错代码保存到变量中
  26. $errorCode = $_FILES['my_pic']['error'];
  27. // 判断 出错代码是否大于0
  28. if ($errorCode > UPLOAD_ERR_OK) {
  29. //如果大于零 则便判断是几
  30. switch ($errorCode) {
  31. case UPLOAD_ERR_INI_SIZE:
  32. throw new UploadException('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值', 1);
  33. break;
  34. case UPLOAD_ERR_FORM_SIZE:
  35. throw new UploadException('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值', 2);
  36. break;
  37. case UPLOAD_ERR_PARTIAL:
  38. throw new UploadException('文件只有部分被上传', 3);
  39. break;
  40. case UPLOAD_ERR_NO_FILE:
  41. throw new UploadException('没有文件被上传', 4);
  42. break;
  43. case UPLOAD_ERR_NO_TMP_DIR:
  44. throw new UploadException('找不到临时文件夹', 6);
  45. break;
  46. case UPLOAD_ERR_CANT_WRITE:
  47. throw new UploadException('文件写入失败', 7);
  48. break;
  49. default:
  50. // 测试时建议关掉default: 避免误报影响
  51. // throw new UploadException('未知类型错误', 8);
  52. }
  53. }
  54. $fileType = $_FILES['my_pic']['type']['0'];
  55. if (@strstr($fileType, '/', true) == ''){
  56. echo '请选择要上传的文件';
  57. }else{
  58. if (strstr($fileType, '/', true) !== 'image') throw new UploadException('文件类型错误',9);
  59. }
  60. if ($_FILES['my_pic'])
  61. // 遍历error 健和值
  62. foreach ($_FILES['my_pic']['error'] as $key => $error) {
  63. // 判断 error 是否=== 0
  64. if ($error === UPLOAD_ERR_OK) {
  65. // 获取临时文件名
  66. $tmpFileName = $_FILES['my_pic']['tmp_name'][$key];
  67. // 获取原始文件名
  68. $originalFileName = $_FILES['my_pic']['name'][$key];
  69. // 要转移动目录和文件名命名
  70. $destFileName = 'uploads/'. $originalFileName;
  71. // 移动文件
  72. move_uploaded_file($tmpFileName, $destFileName);
  73. // 预览
  74. echo "<img src='{$destFileName}' width='200'>";
  75. }
  76. }
  77. }catch (UploadException $e) {
  78. echo $e;
  79. }
  80. ?>
  81. <!DOCTYPE html>
  82. <html lang="en">
  83. <head>
  84. <meta charset="UTF-8">
  85. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  86. <title>文件上传变量$_FILES</title>
  87. </head>
  88. <body>
  89. <hr>
  90. <form action="" method="POST" enctype="multipart/form-data">
  91. <fieldset>
  92. <legend>多文件上传: 逐个上传(二)</legend>
  93. <input type="hidden" name="MAX_FILE_SIZE" value="3000000">
  94. <!-- 将name属性值能数组的形式提供 -->
  95. <input type="file" name="my_pic[]">
  96. <input type="file" name="my_pic[]">
  97. <input type="file" name="my_pic[]">
  98. <button>上传</button>
  99. </fieldset>
  100. </form>
  101. </body>
  102. </html>

上传效果:

错误信息:

没有处理好的地方:

3. 批量上传文件

代码

  1. <?php
  2. // $_FILES
  3. printf('<pre>%s</pre>', print_r($_FILES, true));
  4. // 自定义上传异常类
  5. class UploadException extends Exception
  6. {
  7. // 在异常子类中,可以访问并重写Exception中的四个属性,通过__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. tr:last-of-type td {color: coral;}
  16. </style>
  17. <table>
  18. <tr><td>代码</td><td>信息</td><td>文件</td><td>行号</td></tr>
  19. <tr><td>$this->code</td><td>$this->message</td><td>$this->file</td><td>$this->line</td></tr>
  20. </table>
  21. UPLOAD;
  22. }
  23. }
  24. try {
  25. // 上传出错的代码 将出错代码保存到变量中
  26. $errorCode = $_FILES['my_pic']['error']['0'];
  27. // 判断 出错代码是否大于0
  28. if ($errorCode > UPLOAD_ERR_OK) {
  29. //如果大于零 则便判断是几
  30. switch ($errorCode) {
  31. case UPLOAD_ERR_INI_SIZE:
  32. throw new UploadException('上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值', 1);
  33. break;
  34. case UPLOAD_ERR_FORM_SIZE:
  35. throw new UploadException('上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值', 2);
  36. break;
  37. case UPLOAD_ERR_PARTIAL:
  38. throw new UploadException('文件只有部分被上传', 3);
  39. break;
  40. case UPLOAD_ERR_NO_FILE:
  41. throw new UploadException('没有文件被上传', 4);
  42. break;
  43. case UPLOAD_ERR_NO_TMP_DIR:
  44. throw new UploadException('找不到临时文件夹', 6);
  45. break;
  46. case UPLOAD_ERR_CANT_WRITE:
  47. throw new UploadException('文件写入失败', 7);
  48. break;
  49. default:
  50. // 测试时建议关掉default: 避免误报影响
  51. // throw new UploadException('未知类型错误', 8);
  52. }
  53. }
  54. $fileType = $_FILES['my_pic']['type']['0'];
  55. if (@strstr($fileType, '/', true) == ''){
  56. echo '请选择要上传的文件';
  57. }else{
  58. if (strstr($fileType, '/', true) !== 'image') throw new UploadException('文件类型错误',9);
  59. }
  60. if ($_FILES['my_pic'])
  61. // 遍历error 健和值
  62. foreach ($_FILES['my_pic']['error'] as $key => $error) {
  63. // 判断 error 是否=== 0
  64. if ($error === UPLOAD_ERR_OK) {
  65. // 获取临时文件名
  66. $tmpFileName = $_FILES['my_pic']['tmp_name'][$key];
  67. // 获取原始文件名
  68. $originalFileName = $_FILES['my_pic']['name'][$key];
  69. // 要转移动目录和文件名命名
  70. $destFileName = 'uploads/'. $originalFileName;
  71. // 移动文件
  72. move_uploaded_file($tmpFileName, $destFileName);
  73. // 预览
  74. echo "<img src='{$destFileName}' width='200'>";
  75. }
  76. }
  77. }catch (UploadException $e) {
  78. echo $e;
  79. }
  80. ?>
  81. <!DOCTYPE html>
  82. <html lang="en">
  83. <head>
  84. <meta charset="UTF-8">
  85. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  86. <title>文件上传变量$_FILES</title>
  87. </head>
  88. <body>
  89. <hr>
  90. <form action="" method="POST" enctype="multipart/form-data">
  91. <fieldset>
  92. <legend>多文件上传: 批量上传</legend>
  93. <input type="hidden" name="MAX_FILE_SIZE" value="3000000">
  94. <!-- 将name属性值能数组的形式提供 -->
  95. <input type="file" name="my_pic[]" multiple>
  96. <button>上传</button>
  97. </fieldset>
  98. </form>
  99. </body>
  100. </html>

选择效果:

上传效果:

总结:学习了文件上传的流程步骤以及用到哪些函数,但是不怎么懂怎么封装类。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:不懂封装没关系 , 以后会的, 那是选做
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post