Blogger Information
Blog 48
fans 0
comment 0
visits 34346
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
文件上传异常类处理和文件上传类(0731)
丶久而旧之丶
Original
620 people have browsed it

文件上传

自定义异常类

  1. <?php
  2. // 文件上传自定义异常类
  3. class UploadException extends Exception
  4. {
  5. public function __toString()
  6. {
  7. return <<< UPLOAD
  8. <table>
  9. <tr>
  10. <th>错误编号</th>
  11. <th>错误信息</th>
  12. <th>错误文件</th>
  13. <th>错误行号</th>
  14. </tr>
  15. <tr>
  16. <td>$this->code</td>
  17. <td>$this->message</td>
  18. <td>$this->file</td>
  19. <td>$this->line</td>
  20. </tr>
  21. </table>
  22. <style>
  23. table {border:1px solid black;border-collapse:collapse;text-align:center;}
  24. th,td {border:1px solid black;}
  25. tr:last-of-type {color: red;}
  26. </style>
  27. UPLOAD;
  28. }
  29. }
  30. try {
  31. $fileCode = $_FILES['my_pic']['error'] ?? null;
  32. if ($fileCode && ($fileCode > UPLOAD_ERR_OK)) {
  33. switch ($fileCode) {
  34. case UPLOAD_ERR_INI_SIZE:
  35. throw new UploadException('超过了php.ini中的大小设置', 1);
  36. break;
  37. case UPLOAD_ERR_FORM_SIZE:
  38. throw new UploadException('超过了前端MAX_FILE_SIZE中的大小设置', 2);
  39. break;
  40. case UPLOAD_ERR_PARTIAL:
  41. throw new UploadException('文件只有部分被上传', 3);
  42. break;
  43. case UPLOAD_ERR_NO_FILE:
  44. throw new UploadException('没有文件上传', 4);
  45. break;
  46. case UPLOAD_ERR_NO_TMP_DIR:
  47. throw new UploadException('找不到临时文件夹', 6);
  48. break;
  49. case UPLOAD_ERR_CANT_WRITE:
  50. throw new UploadException('文件写入失败', 7);
  51. break;
  52. default:
  53. throw new UploadException('未知类型错误', 5);
  54. }
  55. }
  56. // 判断文件类型
  57. $type = strstr($_FILES['my_pic']['type'], '/', true) ?? null;
  58. if ($type && ($type !== 'image')) throw new UploadException('不是有效的图片格式', 8);
  59. // 判断是否是POst方式上传
  60. $tmp_name = $_FILES['my_pic']['tmp_name'] ?? null;
  61. if ($tmp_name && (!(is_uploaded_file($tmp_name)))) throw new UploadException('不是有效的上传方式', 9);
  62. } catch (UploadException $e) {
  63. echo $e;
  64. }
  65. ?>
  66. <!DOCTYPE html>
  67. <html lang="en">
  68. <head>
  69. <meta charset="UTF-8">
  70. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  71. <title>单文件上传</title>
  72. </head>
  73. <body>
  74. <form action=" " method="POST" enctype="multipart/form-data">
  75. <fieldset>
  76. <legend>文件上传</legend>
  77. <!-- 前端设置上传文件大小 -->
  78. <!-- 设置大小必须写在input:file前面 如果反了就没有效果 -->
  79. <input type="hidden" name="MAX_FILE_SIZE" value="10000">
  80. <input type="file" name="my_pic" id="">
  81. <button>上传</button>
  82. </fieldset>
  83. </form>
  84. </body>
  85. </html>

文件移动

  1. <?php
  2. $tmp_name = $_FILES['my_pic']['tmp_name'] ?? null;
  3. if ($tmp_name) {
  4. if (is_uploaded_file($tmp_name)) {
  5. // 获取原始文件名
  6. $fileName = $_FILES['my_pic']['name'];
  7. // 生成唯一的文件名
  8. $destFileName = 'file/' . 'PHP中文网' . sha1(time() . mt_rand(1, 1000) . $fileName) . strstr($fileName, '.');
  9. // 移动文件到正式目录
  10. move_uploaded_file($tmp_name, $destFileName);
  11. echo '<p>' . $_FILES['my_pic']['name'] . ':上传成功</p>';
  12. echo "<img src='{$destFileName}' width='200px'>";
  13. } else {
  14. throw new UploadException('不是有效的上传方式', 9);
  15. }
  16. }
  17. } catch (UploadException $e) {
  18. echo $e;
  19. }
  20. ?>
  21. ?>
  22. <!DOCTYPE html>
  23. <html lang="en">
  24. <head>
  25. <meta charset="UTF-8">
  26. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  27. <title>单文件上传</title>
  28. </head>
  29. <body>
  30. <form action=" " method="POST" enctype="multipart/form-data">
  31. <fieldset>
  32. <legend>文件上传</legend>
  33. <!-- 前端设置上传文件大小 -->
  34. <!-- 设置大小必须写在input:file前面 如果反了就没有效果 -->
  35. <!-- <input type="hidden" name="MAX_FILE_SIZE" value="10000"> -->
  36. <input type="file" name="my_pic" id="">
  37. <button>上传</button>
  38. </fieldset>
  39. </form>
  40. </body>
  41. </html>

多文件上传

  1. <?php
  2. // 文件上传自定义异常类
  3. printf('<pre>%s</pre>', print_r($_FILES, true));
  4. class UploadException extends Exception
  5. {
  6. public function __toString()
  7. {
  8. return <<< UPLOAD
  9. <table>
  10. <tr>
  11. <th>错误编号</th>
  12. <th>错误信息</th>
  13. <th>错误文件</th>
  14. <th>错误行号</th>
  15. </tr>
  16. <tr>
  17. <td>$this->code</td>
  18. <td>$this->message</td>
  19. <td>$this->file</td>
  20. <td>$this->line</td>
  21. </tr>
  22. </table>
  23. <style>
  24. table {border:1px solid black;border-collapse:collapse;text-align:center;}
  25. th,td {border:1px solid black;}
  26. tr:last-of-type {color: red;}
  27. </style>
  28. UPLOAD;
  29. }
  30. }
  31. try {
  32. $error = $_FILES['my_pic']['error'];
  33. // print_r($error);
  34. // die;
  35. if ($error)
  36. foreach ($error as $vaule) {
  37. $fileCode = $vaule;
  38. if ($fileCode && ($fileCode > UPLOAD_ERR_OK)) {
  39. switch ($fileCode) {
  40. case UPLOAD_ERR_INI_SIZE:
  41. throw new UploadException('超过了php.ini中的大小设置', 1);
  42. break;
  43. case UPLOAD_ERR_FORM_SIZE:
  44. throw new UploadException('超过了前端MAX_FILE_SIZE中的大小设置', 2);
  45. break;
  46. case UPLOAD_ERR_PARTIAL:
  47. throw new UploadException('文件只有部分被上传', 3);
  48. break;
  49. case UPLOAD_ERR_NO_FILE:
  50. throw new UploadException('没有文件上传', 4);
  51. break;
  52. case UPLOAD_ERR_NO_TMP_DIR:
  53. throw new UploadException('找不到临时文件夹', 6);
  54. break;
  55. case UPLOAD_ERR_CANT_WRITE:
  56. throw new UploadException('文件写入失败', 7);
  57. break;
  58. default:
  59. throw new UploadException('未知类型错误', 5);
  60. }
  61. }
  62. };
  63. // 判断文件类型
  64. $type = ($_FILES['my_pic']['type']);
  65. if ($type)
  66. foreach ($type as $key => $vaule) {
  67. $type = strstr($vaule, '/', true);
  68. $tmp_name = $_FILES['my_pic']['tmp_name'][$key];
  69. if ($type === 'image') {
  70. // 判断上传方式是否正确
  71. if (is_uploaded_file($tmp_name)) {
  72. $fileName = $_FILES['my_pic']['name'][$key];
  73. // 生成唯一的文件名
  74. $destFileName = 'file/' . 'PHP中文网' . sha1(time() . mt_rand(1, 1000) . $fileName) . strstr($fileName, '.');
  75. move_uploaded_file($tmp_name, $destFileName);
  76. echo '<p>' . $fileName . ':上传成功</p>';
  77. echo "<img src='{$destFileName}' width='200px'>";
  78. } else {
  79. throw new UploadException('不是有效的上传方式', 9);
  80. }
  81. } else {
  82. throw new UploadException('不是有效的图片格式', 8);
  83. }
  84. }
  85. } catch (UploadException $e) {
  86. echo $e;
  87. }
  88. ?>
  89. <!DOCTYPE html>
  90. <html lang="en">
  91. <head>
  92. <meta charset="UTF-8">
  93. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  94. <title>多文件上传</title>
  95. </head>
  96. <body>
  97. <form action=" " method="POST" enctype="multipart/form-data">
  98. <fieldset>
  99. <legend>多文件上传</legend>
  100. <!-- 前端设置上传文件大小 -->
  101. <!-- 设置大小必须写在input:file前面 如果反了就没有效果 -->
  102. <!-- <input type="hidden" name="MAX_FILE_SIZE" value="270000"> -->
  103. <input type="file" name="my_pic[]" multiple id="">
  104. <button>上传</button>
  105. </fieldset>
  106. </form>
  107. </body>

文件上传类

  1. <?php
  2. class UploadException extends Exception
  3. {
  4. public function __toString()
  5. {
  6. return <<< UPLOAD
  7. <table>
  8. <tr>
  9. <th>错误编号</th>
  10. <th>错误信息</th>
  11. <th>错误文件</th>
  12. <th>错误行号</th>
  13. </tr>
  14. <tr>
  15. <td>$this->code</td>
  16. <td>$this->message</td>
  17. <td>$this->file</td>
  18. <td>$this->line</td>
  19. </tr>
  20. </table>
  21. <style>
  22. table {border:1px solid black;border-collapse:collapse;text-align:center;}
  23. th,td {border:1px solid black;}
  24. tr:last-of-type {color: red;}
  25. </style>
  26. UPLOAD;
  27. }
  28. }
  29. class Upload
  30. {
  31. private $fileCode;
  32. private $type;
  33. private $name;
  34. private $tmp_name;
  35. // 正式文件目录名
  36. private $file_name;
  37. public function __construct($name)
  38. {
  39. $this->file_name = $name;
  40. // 循环获得上传文件的各个信息
  41. foreach ($_FILES as $v) {
  42. $this->fileCode = $v['error'];
  43. $this->type = $v['type'];
  44. $this->tmp_name = $v['tmp_name'];
  45. $this->name = $v['name'];
  46. $this->get();
  47. $this->type();
  48. $this->tmp_name();
  49. }
  50. }
  51. private function get()
  52. {
  53. // 根据错误信息抛出相应错误
  54. if ($this->fileCode && ($this->fileCode > UPLOAD_ERR_OK)) {
  55. switch ($this->fileCode) {
  56. case UPLOAD_ERR_INI_SIZE:
  57. throw new UploadException('超过了php.ini中的大小设置', 1);
  58. break;
  59. case UPLOAD_ERR_FORM_SIZE:
  60. throw new UploadException('超过了前端MAX_FILE_SIZE中的大小设置', 2);
  61. break;
  62. case UPLOAD_ERR_PARTIAL:
  63. throw new UploadException('文件只有部分被上传', 3);
  64. break;
  65. case UPLOAD_ERR_NO_FILE:
  66. throw new UploadException('没有文件上传', 4);
  67. break;
  68. case UPLOAD_ERR_NO_TMP_DIR:
  69. throw new UploadException('找不到临时文件夹', 6);
  70. break;
  71. case UPLOAD_ERR_CANT_WRITE:
  72. throw new UploadException('文件写入失败', 7);
  73. break;
  74. default:
  75. throw new UploadException('未知类型错误', 5);
  76. }
  77. }
  78. }
  79. private function type()
  80. {
  81. // 判断文件的格式
  82. $typ = strstr($this->type, '/', true);
  83. if ($this->type && ($typ !== 'image')) throw new UploadException('不是有效的图片格式', 8);
  84. }
  85. private function tmp_name()
  86. {
  87. // 判断文件上传方式是否为POST,正确则把文件转入正式目录
  88. if ($this->tmp_name) {
  89. if (is_uploaded_file($this->tmp_name)) {
  90. // 生成唯一文件名
  91. $destFileName = $this->file_name . 'PHP中文网' . sha1(time() . mt_rand(1, 1000) . $this->name) . strstr($this->name, '.');
  92. // 把文件转入正式目录
  93. move_uploaded_file($this->tmp_name, $destFileName);
  94. echo '<p>' . $this->name . ':上传成功</p>';
  95. echo "<img src='{$destFileName}' width='200px'>";
  96. } else {
  97. throw new UploadException('不是有效的上传方式', 9);
  98. }
  99. }
  100. }
  101. }
  102. try {
  103. printf('<pre>%s</pre>', print_r($_FILES, true));
  104. $a = new Upload('123456/');
  105. } catch (UploadException $e) {
  106. echo $e;
  107. }
  108. ?>
  109. <!DOCTYPE html>
  110. <html lang="en">
  111. <head>
  112. <meta charset="UTF-8">
  113. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  114. <title>单文件上传</title>
  115. </head>
  116. <body>
  117. <form action=" " method="POST" enctype="multipart/form-data">
  118. <fieldset>
  119. <legend>单文件上传</legend>
  120. <!-- 前端设置上传文件大小 -->
  121. <!-- 设置大小必须写在input:file前面 如果反了就没有效果 -->
  122. <!-- <input type="hidden" name="MAX_FILE_SIZE" value="10000"> -->
  123. <input type="file" name="my_pic1" id="">
  124. <input type="file" name="my_pic2" id="">
  125. <input type="file" name="my_pic3" id="">
  126. <button>上传</button>
  127. </fieldset>
  128. </form>
  129. </form>
  130. </body>
  131. </html>

总结

1.了解了文件上传的错误处理
2.如果想用一个input标签完成多文件上传,使用了较多的流程控制但不知应该如果精简

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