Blogger Information
Blog 24
fans 0
comment 0
visits 18694
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
单文件上传、多文件上传、批量上传
昔年
Original
743 people have browsed it

文件上传

1.单文件上传

  1. <?php
  2. // $_FILES
  3. printf('<pre>%s</pre>', print_r($_FILES, true));
  4. //自定义异常类
  5. // 自定义上传异常类
  6. class UploadException extends Exception
  7. {
  8. // 在异常子类中,可以访问并重写Exception中的四个属性,通过__toString()格式化异常输出信息
  9. public function __toString()
  10. {
  11. return <<< UPLOAD
  12. <style>
  13. table {border-collapse: collapse;border:1px solid black;text-align: center;}
  14. td {border:1px solid black;padding: 5px;}
  15. tr:first-of-type {background-color:#eee;}
  16. tr:last-of-type td {color: coral;}
  17. </style>
  18. <table>
  19. <tr><td>代码</td><td>信息</td><td>文件</td><td>行号</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. try {
  27. //上传出错的代码
  28. $errorCode = $_FILES['my_pic']['error'];
  29. if ($errorCode > UPLOAD_ERR_OK) {
  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('文件大小超过表单中`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_CANT_WRITE:
  44. throw new UploadException('找不到临时文件夹', 6);
  45. break;
  46. case UPLOAD_ERR_CANT_WRITE:
  47. throw new UploadException('文件写入失败', 7);
  48. break;
  49. default:
  50. throw new UploadException('未知类型错误', 8);
  51. break;
  52. }
  53. }
  54. $fileType = $_FILES['my_pic']['type'];
  55. // var_dump(strstr($fileType, '/', true));
  56. if (isset($fileType) && strstr($fileType, '/', true) !== 'image') throw new UploadException('文件类型错误', 9);
  57. //将文件从临时目录移动到用户自定义的目标目录中
  58. $tmpFileName = $_FILES['my_pic']['tmp_name'];
  59. if (is_uploaded_file($tmpFileName)) {
  60. //获取原始文件名
  61. $originalFileName = $_FILES['my_pic']['name'];
  62. //生成目标文件名
  63. $destFileName = 'uploads/' . md5(mt_rand(1, 1000) . time()) . strstr($originalFileName, '.');
  64. if (move_uploaded_file($tmpFileName, $destFileName)) {
  65. echo "<p>$originalFileName:文件上传成功~~</p>";
  66. echo "<img src='{$destFileName}' width='200'>";
  67. }
  68. }
  69. } catch (UploadException $e) {
  70. echo $e;
  71. }
  72. ?>
  73. <!DOCTYPE html>
  74. <html lang="en">
  75. <head>
  76. <meta charset="UTF-8">
  77. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  78. <title>文件上传变量$_FILES</title>
  79. </head>
  80. <body>
  81. <hr>
  82. <form action="" method="post" enctype="multipart/form-data">
  83. <fieldset>
  84. <legend>单文件上传:文件移动</legend>
  85. <input type="hidden" name="MAX_FILE_SIZE" value="3000000">
  86. <input type="file" name="my_pic" id="">
  87. <button>上传</button>
  88. </fieldset>
  89. </form>
  90. </body>
  91. </html>

2.多文件上传

  1. <?php
  2. // $_FILES
  3. printf('<pre>%s</pre>', print_r($_FILES, true));
  4. $files = array_pop($_FILES);
  5. if (is_array($files)) {
  6. foreach ($files['error'] as $key => $error) {
  7. if ($error === UPLOAD_ERR_OK) {
  8. //文件类型检查
  9. $fileType = $files['type'][$key];
  10. $types = ['image/jpeg', 'image/png', 'image/gif'];
  11. if (in_array($fileType, $types)) {
  12. $originalFilName = $files['name'][$key];
  13. $tmpFileName = $files['tmp_name'][$key];
  14. //目标文件名称
  15. $destFileName = 'uploads/' . md5(mt_rand(1, 1000) . time()) . strstr($originalFilName, '.', true);
  16. move_uploaded_file($tmpFileName, $destFileName);
  17. echo "<img src='{$destFileName}' width='200'>";
  18. }
  19. }
  20. }
  21. }
  22. ?>
  23. <!DOCTYPE html>
  24. <html lang="en">
  25. <head>
  26. <meta charset="UTF-8">
  27. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  28. <title>文件上传变量$_FILES</title>
  29. </head>
  30. <body>
  31. <hr>
  32. <form action="" method="post" enctype="multipart/form-data">
  33. <fieldset>
  34. <legend>多文件上传:逐个上传(二)</legend>
  35. <input type="hidden" name="MAX_FILE_SIZE" value="3000000">
  36. <input type="file" name="my_pic[]" id="">
  37. <input type="file" name="my_pic[]" id="">
  38. <input type="file" name="my_pic[]" id="">
  39. <button>上传</button>
  40. </fieldset>
  41. </form>
  42. </body>
  43. </html>

3.批量文件上传

  1. <?php
  2. // $_FILES
  3. printf('<pre>%s</pre>', print_r($_FILES, true));
  4. $files = array_pop($_FILES);
  5. if (is_array($files)) {
  6. foreach ($files['error'] as $key => $error) {
  7. if ($error === UPLOAD_ERR_OK) {
  8. $originalFileName = $files['name'][$key];
  9. $tmpFileName = $files['tmp_name'][$key];
  10. $destFileName = 'uploads/' . md5(mt_rand(1, 1000) . time()) . strstr($originalFileName, '.', true);
  11. move_uploaded_file($tmpFileName, $destFileName);
  12. echo "<img src='{$destFileName}' width='200'>";
  13. }
  14. }
  15. }
  16. ?>
  17. <!DOCTYPE html>
  18. <html lang="en">
  19. <head>
  20. <meta charset="UTF-8">
  21. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  22. <title>文件上传变量$_FILES</title>
  23. </head>
  24. <body>
  25. <hr>
  26. <form action="" method="post" enctype="multipart/form-data">
  27. <fieldset>
  28. <legend>单文件上传:文件移动</legend>
  29. <input type="hidden" name="MAX_FILE_SIZE" value="3000000">
  30. <input type="file" name="my_pic[]" id="" multiple>
  31. <button>上传</button>
  32. </fieldset>
  33. </form>
  34. </body>
  35. </html>

总结:文件上传最主要的就是要用到超全局变量$_FILE,所有的文件上传信息都在这个数组里面。文件上传有7中类型编码,没有5,从0-7分别对应的是“文件上传成功”、“文件超过php.iniupload_max_filesize值”、“文件超过表单中MAX_FILE_SIZE指定的值”、“文件只有部分上传”、“文件没有被上传”、“找不到临时文件夹”、“文件写入失败”。判定时也可以用对应的常量来判定。

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!