Blogger Information
Blog 100
fans 8
comment 2
visits 150056
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
单文件上传和多文件上传
lilove的博客
Original
1237 people have browsed it

单文件上传

  • 单文件完整代码:
  1. <?php
  2. // 自定文件上传异常处理类
  3. class UploadException extends Exception
  4. {
  5. public function __toString()
  6. {
  7. return <<< UPLOAD
  8. <style>
  9. table {text-align:center;border:1px solid black;border-collapse:collapse;}
  10. th,td {padding:5px;border:1px solid black;}
  11. tr:hover {background-color:rgb(240,240,240);}
  12. td {color:coral;}
  13. </style>
  14. <table>
  15. <tr>
  16. <th>编号</th>
  17. <th>信息</th>
  18. <th>文件</th>
  19. <th>行号</th>
  20. </tr>
  21. <tr>
  22. <td>$this->code</td>
  23. <td>$this->message</td>
  24. <td>$this->file</td>
  25. <td>$this->line</td>
  26. </tr>
  27. </table>
  28. UPLOAD;
  29. }
  30. }
  31. // 捕捉异常
  32. try {
  33. // 对错误编码的判断
  34. $errorCode = $_FILES['img']['error'] ?? null;
  35. // 使用系统常量进行判断
  36. if ($errorCode > UPLOAD_ERR_OK) {
  37. switch ($errorCode) {
  38. case UPLOAD_ERR_INI_SIZE:
  39. throw new uploadException('文件超过`php.ini`中`upload_max_filesize`值', 1);
  40. break;
  41. case UPLOAD_ERR_FORM_SIZE:
  42. throw new UploadException('文件大小超过表单中`MAX_FILE_SIZE`指定的值', 2);
  43. break;
  44. case UPLOAD_ERR_PARTIAL:
  45. throw new UploadException('文件只有部分被上传', 3);
  46. break;
  47. case UPLOAD_ERR_NO_FILE:
  48. throw new UploadException('没有文件被上传', 4);
  49. break;
  50. case UPLOAD_ERR_NO_TMP_DIR:
  51. throw new UploadException('找不到临时文件夹', 6);
  52. break;
  53. case UPLOAD_ERR_CANT_WRITE:
  54. throw new UploadException('文件写入失败', 7);
  55. break;
  56. default:
  57. throw new UploadException('未知错误', 8);
  58. }
  59. }
  60. // 文件类型检查
  61. // 原始文件名
  62. $fileType = $_FILES['img']['type'] ?? null;
  63. // 切割字符串strstr(),获取文件类型
  64. $type = strstr($fileType, '/', true);
  65. if (!is_null($fileType)) {
  66. if ($type !== 'image') {
  67. throw new UploadException('文件类型错误', 9);
  68. }
  69. }
  70. // 上传方式检查
  71. // 临时文件名
  72. $tmpFileName = $_FILES['img']['tmp_name'] ?? null;
  73. // 临时文件是存在的,并且是POST上传
  74. if ($tmpFileName && is_uploaded_file($tmpFileName)) {
  75. // 将文件从临时目录移到目标目录move_uploaded_file(临时文件名,目标文件名)
  76. // 原始文件名
  77. $originalFileName = $_FILES['img']['name'] ?? null;
  78. // 文件扩展名
  79. $extension = strstr($originalFileName, '.');
  80. // 目标文件名加密
  81. $destFileName = 'uploads/' . md5(time() . mt_rand(1, 1000)) . strstr($originalFileName, '.');
  82. if (move_uploaded_file($tmpFileName, $destFileName)) {
  83. echo '<p>' . $_FILES['img']['name'] . ':文件上传成功' . '</p>';
  84. // 上传成功后预览
  85. echo "<img src='{$destFileName}' width='200'>";
  86. }
  87. }
  88. } catch (UploadException $e) {
  89. echo $e;
  90. }
  91. ?>
  92. <!DOCTYPE html>
  93. <html lang="zh-cn">
  94. <head>
  95. <meta charset="UTF-8">
  96. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  97. <title>文件上传成功后从临时目录移到目标目录</title>
  98. </head>
  99. <body>
  100. <!-- 上传文件必须的表单属性:method="POST" enctype="multipart/form-data" -->
  101. <form action="" method="POST" enctype="multipart/form-data">
  102. <fieldset>
  103. <legend>单文件上传</legend>
  104. <!-- 隐藏域设置上传文件大小必须写到文件上传标签前面 -->
  105. <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
  106. <input type="file" name="img" id="">
  107. <button type="submit">提交上传</button>
  108. </fieldset>
  109. </form>
  110. </body>
  • 运行效果:

  • 上传成功:

  • 上传失败:


多文件上传

  • 多文件完整代码
  1. <?php
  2. // 自定文件上传异常处理类
  3. class UploadException extends Exception
  4. {
  5. public function __toString()
  6. {
  7. return <<< UPLOAD
  8. <style>
  9. table {text-align:center;border:1px solid black;border-collapse:collapse;}
  10. th,td {padding:5px;border:1px solid black;}
  11. tr:hover {background-color:rgb(240,240,240);}
  12. td {color:coral;}
  13. </style>
  14. <table>
  15. <tr>
  16. <th>编号</th>
  17. <th>信息</th>
  18. <th>文件</th>
  19. <th>行号</th>
  20. </tr>
  21. <tr>
  22. <td>$this->code</td>
  23. <td>$this->message</td>
  24. <td>$this->file</td>
  25. <td>$this->line</td>
  26. </tr>
  27. </table>
  28. UPLOAD;
  29. }
  30. }
  31. // 捕获异常
  32. try {
  33. // 遍历上传文件
  34. foreach ($_FILES as $file) {
  35. // print_r($file);
  36. // 对错误编码的判断
  37. $errorCode = $file['error'] ?? null;
  38. // 使用系统常量进行判断
  39. if ($errorCode > UPLOAD_ERR_OK) {
  40. switch ($errorCode) {
  41. case UPLOAD_ERR_INI_SIZE:
  42. throw new uploadException('文件超过`php.ini`中`upload_max_filesize`值', 1);
  43. break;
  44. case UPLOAD_ERR_FORM_SIZE:
  45. throw new UploadException('文件大小超过表单中`MAX_FILE_SIZE`指定的值', 2);
  46. break;
  47. case UPLOAD_ERR_PARTIAL:
  48. throw new UploadException('文件只有部分被上传', 3);
  49. break;
  50. case UPLOAD_ERR_NO_FILE:
  51. throw new UploadException('没有文件被上传', 4);
  52. break;
  53. case UPLOAD_ERR_NO_TMP_DIR:
  54. throw new UploadException('找不到临时文件夹', 6);
  55. break;
  56. case UPLOAD_ERR_CANT_WRITE:
  57. throw new UploadException('文件写入失败', 7);
  58. break;
  59. default:
  60. throw new UploadException('未知错误', 8);
  61. }
  62. }
  63. // 文件类型检查
  64. // 原始文件名
  65. $fileType = $file['type'] ?? null;
  66. // 切割字符串strstr(),获取文件类型
  67. $type = strstr($fileType, '/', true);
  68. if (!is_null($fileType)) {
  69. if ($type !== 'image') {
  70. throw new UploadException('文件类型错误', 9);
  71. }
  72. }
  73. // 上传方式检查
  74. // 临时文件名
  75. $tmpFileName = $file['tmp_name'] ?? null;
  76. // 临时文件是存在的,并且是POST上传
  77. if ($tmpFileName && is_uploaded_file($tmpFileName)) {
  78. // 将文件从临时目录移到目标目录move_uploaded_file(临时文件名,目标文件名)
  79. // 原始文件名
  80. $originalFileName = $file['name'] ?? null;
  81. // 文件扩展名
  82. $extension = strstr($originalFileName, '.');
  83. // 目标文件名加密
  84. $destFileName = 'uploads/' . md5(time() . mt_rand(1, 1000)) . strstr($file['name'], '.');
  85. if (move_uploaded_file($tmpFileName, $destFileName)) {
  86. echo '<p>' . $originalFileName . ':文件上传成功' . '</p>';
  87. // 上传成功后预览
  88. echo "<img src='{$destFileName}' width='200'>";
  89. }
  90. }
  91. }
  92. } catch (UploadException $e) {
  93. echo $e;
  94. }
  95. ?>
  96. <!DOCTYPE html>
  97. <html lang="zh-cn">
  98. <head>
  99. <meta charset="UTF-8">
  100. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  101. <title>多文件上传</title>
  102. </head>
  103. <body>
  104. <!-- 上传文件必须的表单属性:method="POST" enctype="multipart/form-data" -->
  105. <form action="" method="POST" enctype="multipart/form-data">
  106. <fieldset>
  107. <legend>多文件上传</legend>
  108. <!-- 隐藏域设置上传文件大小必须写到文件上传标签前面 -->
  109. <input type="hidden" name="MAX_FILE_SIZE" value="1000000">
  110. <!-- 多文件的name属性值不能一样 -->
  111. <input type="file" name="img1" id="">
  112. <input type="file" name="img2" id="">
  113. <input type="file" name="img3" id="">
  114. <hr>
  115. <button type="submit">提交上传</button>
  116. </fieldset>
  117. </form>
  118. </body>
  • 运行效果

  • 上传成功(必须全部上传成功):

  • 上传失败(只要失败一个文件所有都无法上传):


总结

1.前端隐藏域限制文件大小要放在上传表单前面;
2.后端也要验证文件大小及类型;
3.多文件上传的表单 name 属性值不能一样;
4.文件上传要用 POST 方式;
5.上传成功后的文件名要用加密方式区别开,以防同名文件重复上传覆盖。

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