博客列表 >多文件上传

多文件上传

晨
原创
2022年05月06日 09:31:32396浏览

方式1代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. foreach ($_FILES as $file) {
  5. if ($file['error'] === 0) {
  6. $destFile = 'uploads/' . $file['name'];
  7. move_uploaded_file($file['tmp_name'],$destFile);
  8. echo "<img src='$destFile' width='230'>";
  9. }
  10. }
  11. ?>
  12. <head>
  13. <meta charset="UTF-8">
  14. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  15. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  16. <title>多文件上传</title>
  17. <style>
  18. .upload {
  19. display: grid;
  20. grid-template-columns: repeat(1,1fr);
  21. gap: 3vw;
  22. padding: 0 15%;
  23. }
  24. .upload button {
  25. width: 120px;
  26. }
  27. </style>
  28. </head>
  29. <body>
  30. <form action="" method="POST" enctype="multipart/form-data">
  31. <fieldset class="upload">
  32. <legend>多文件上传:逐个上传</legend>
  33. <input type="file" name="mydoc1">
  34. <input type="file" name="mydoc2">
  35. <input type="file" name="mydoc3">
  36. <button>上传</button>
  37. </fieldset>
  38. </form>
  39. </body>
  40. </html>

方式2代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. printf('<pre>%s</pre>', print_r($_FILES, true));
  5. if (isset($_FILES['mypic'])) {
  6. foreach ($_FILES['mypic']['error'] as $key => $error) {
  7. if ($error === 0) {
  8. $tmpName = $_FILES['mypic']['tmp_name'][$key];
  9. $name = $_FILES['mypic']['name'][$key];
  10. $destFile = 'uploads/' . $name;
  11. move_uploaded_file($tmpName,$destFile);
  12. echo "<img src='$destFile' width='240'>";
  13. }
  14. }
  15. }
  16. ?>
  17. <head>
  18. <meta charset="UTF-8">
  19. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  20. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  21. <title>Document多文件上传2</title>
  22. </head>
  23. <body>
  24. <form action="" method="POST" enctype="multipart/form-data">
  25. <fieldset>
  26. <legend>多文件上传:逐个上传2</legend>
  27. <input type="file" name="mypic[]">
  28. <input type="file" name="mypic[]">
  29. <input type="file" name="mypic[]">
  30. <button>上传</button>
  31. </fieldset>
  32. </form>
  33. </body>
  34. </html>

方式3代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <?php
  4. printf('<pre>%s</pre>', print_r($_FILES, true));
  5. if (isset($_FILES['mypic'])) {
  6. foreach ($_FILES['mypic']['error'] as $key => $error) {
  7. if ($error === 0) {
  8. $tmpName = $_FILES['mypic']['tmp_name'][$key];
  9. $name = $_FILES['mypic']['name'][$key];
  10. $destFile = 'uploads/' . $name;
  11. move_uploaded_file($tmpName,$destFile);
  12. echo "<img src='$destFile' width='240'>";
  13. }
  14. }
  15. }
  16. ?>
  17. <head>
  18. <meta charset="UTF-8">
  19. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  20. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  21. <title>Document多文件上传3</title>
  22. </head>
  23. <body>
  24. <form action="" method="POST" enctype="multipart/form-data">
  25. <fieldset>
  26. <legend>多文件上传:批量上传</legend>
  27. <input type="file" name="mypic[]" multiple>
  28. <button>上传</button>
  29. </fieldset>
  30. </form>
  31. </body>
  32. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议