Blogger Information
Blog 36
fans 1
comment 0
visits 26401
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
实例演示 PHP 单文件上传和多文件批量上传
早晨
Original
727 people have browsed it

单文件上传代码

  1. <?php
  2. // $_FILES
  3. // printf('<pre>%s</pre>', print_r($_FILES, true));
  4. if (isset($_FILES['imgs'])) {
  5. // 原始文件名
  6. $name = $_FILES['imgs']['name'];
  7. // 临时文件名
  8. $tmpName = $_FILES['imgs']['tmp_name'];
  9. // 错误代码
  10. $error = $_FILES['imgs']['error'];
  11. if ($error > 0) {
  12. $img_t = '<span style="color:red">上传失败!</span><br>';
  13. switch ($error) {
  14. case 1:
  15. $img_t .= '大小超过了php.ini中的允许上传的文件大小';
  16. break;
  17. case 2:
  18. $img_t .= '大小超过了表单中(MAX_FILE_SIZE)的允许上传的文件大小';
  19. break;
  20. case 3:
  21. $img_t .= '文件只有部分被上传';
  22. break;
  23. case 4:
  24. $img_t .= '没有文件被上传';
  25. break;
  26. case 6:
  27. $img_t .= '找不到临时目录';
  28. break;
  29. case 7:
  30. $img_t .= '文件写入失败,请检查目录权限';
  31. break;
  32. }
  33. echo "<p>$img_t</p>";
  34. } else {
  35. //判断上传方式是否合法? POST
  36. if (is_uploaded_file($tmpName)) {
  37. // 文件类型白名单
  38. $all_img = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];
  39. // 获取扩展名
  40. $ext_name = pathinfo($name)['extension'];
  41. if (in_array($ext_name, $all_img)) {
  42. // 如果上传方式合法,且是允许的类型,则可以移动到指定的目录中了
  43. $path = 'attach/';
  44. // 为了防止同名覆盖,应该给目标文件名重命名
  45. $img_name = $path . md5($name) . time() . '.' . $ext_name;
  46. // 将文件从临时目录移动到目标目录中
  47. if (move_uploaded_file($tmpName, $img_name)) {
  48. echo '<p>上传成功</p>';
  49. // 预览
  50. echo "<img src='$img_name' width='50px'>";
  51. } else {
  52. }
  53. } else {
  54. echo '<p>文件类型错误</p>';
  55. }
  56. } else {
  57. echo '<p>上传方式非法</p>';
  58. }
  59. }
  60. }
  61. ?>
  62. <!DOCTYPE html>
  63. <html lang="zh-CN">
  64. <head>
  65. <meta charset="UTF-8">
  66. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  67. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  68. <title>单文件上传</title>
  69. </head>
  70. <body>
  71. <!--
  72. 1. method: POST
  73. 2. ecntype: multipart/form-data
  74. -->
  75. <form action="" method="POST" enctype="multipart/form-data">
  76. <fieldset>
  77. <legend>单文件上传</legend>
  78. <input type="hidden" name="MAX_FILE_SIZE" value="200000">
  79. <input type="file" name="imgs">
  80. <button>上传</button>
  81. </fieldset>
  82. </form>
  83. </body>
  84. </html>

运行效果

批量多文件上传代码

  1. <?php
  2. // $_FILES
  3. // printf('<pre>%s</pre>', print_r($_FILES, true));
  4. if (isset($_FILES['imgs'])) {
  5. foreach ($_FILES['imgs']['error'] as $key => $error) {
  6. // 0 => UPLOAD_ERR_OK
  7. if ($error === UPLOAD_ERR_OK) {
  8. // 原始文件名
  9. $name = $_FILES['imgs']['name'][$key];
  10. // 临时文件名
  11. $tmpName = $_FILES['imgs']['tmp_name'][$key];
  12. // 判断上传方式是否合法? POST
  13. if (is_uploaded_file($tmpName)) {
  14. // 文件类型白名单
  15. $all_img = ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'webp'];
  16. // 获取扩展名
  17. $ext_name = pathinfo($name)['extension'];
  18. if (in_array($ext_name, $all_img)) {
  19. // 如果上传方式合法,且是允许的类型,则可以移动到指定的目录中了
  20. $path = 'attach/';
  21. // 为了防止同名覆盖,应该给目标文件名重命名
  22. $img_name = $path . md5($name) . time() . '.' . $ext_name;
  23. // 将文件从临时目录移动到目标目录中
  24. if (move_uploaded_file($tmpName, $img_name)) {
  25. echo '<p>上传成功</p>';
  26. // 预览
  27. echo "<img src='$img_name' width='50px'>";
  28. } else {
  29. }
  30. } else {
  31. echo '<p>文件类型错误</p>';
  32. }
  33. } else {
  34. echo '<p>上传方式非法</p>';
  35. }
  36. }
  37. }
  38. }
  39. ?>
  40. <!DOCTYPE html>
  41. <html lang="zh-CN">
  42. <head>
  43. <meta charset="UTF-8">
  44. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  45. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  46. <title>批量多文件上传</title>
  47. </head>
  48. <body>
  49. <form action="" method="POST" enctype="multipart/form-data">
  50. <fieldset>
  51. <legend>批量多文件上传</legend>
  52. <!-- multiple: 允许同时选择多个 -->
  53. <input type="file" name="imgs[]" multiple>
  54. <button>上传</button>
  55. </fieldset>
  56. </form>
  57. </body>
  58. </html>

运行效果

Correcting teacher:PHPzPHPz

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