Blogger Information
Blog 94
fans 0
comment 0
visits 92686
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
【PHP】PHP 文件上传
可乐随笔
Original
1939 people have browsed it

PHP 文件上传

1. 单文件上传

  1. <?php
  2. $imgs = [];
  3. //遍历:实现单文件/多文件上传
  4. foreach ($_FILES as $file) {
  5. // error = 0 : 表示上传成功
  6. if ($file['error'] == 0) {
  7. //判断文件类型是否正确
  8. if (strstr($file['type'], '/', true) != 'image') {
  9. $tips = '文件类型错误';
  10. continue;
  11. } else {
  12. //创建目标文件名,只要保存不重名
  13. $targetName = 'uploads/' . md5($file['name']) . strstr($file['name'], '.');
  14. //将文件从临时目录移动到目标目录
  15. if (!move_uploaded_file($file['tmp_name'], $targetName)) {
  16. $tips = '文件移动失败';
  17. } else {
  18. //将上传的目标文件名保存到一个数组,供当前页面进行预览
  19. $imgs[] = $targetName;
  20. }
  21. }
  22. }
  23. }
  24. printf('<pre>%s</pre>', print_r($imgs, true));
  25. ?>
  26. <!DOCTYPE html>
  27. <html lang="en">
  28. <head>
  29. <meta charset="UTF-8">
  30. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  31. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  32. <title>文件上传</title>
  33. </head>
  34. <body>
  35. <!--
  36. 文件上传表单的属性要求
  37. 1.action : 为空,表示上传到当前页面
  38. 2.method : post
  39. 3.enctype : multipart/form-data"
  40. -->
  41. <form action="" method="post" enctype="multipart/form-data">
  42. <fieldset>
  43. <legend>文件上传</legend>
  44. <input type="file" name="my_pic1">
  45. <input type="file" name="my_pic2">
  46. <input type="file" name="my_pic3">
  47. <button>上传</button>
  48. </fieldset>
  49. </form>
  50. <!-- 判断是否有错误 -->
  51. <?php if (isset($tips)) : ?>
  52. <!-- 显示错误 -->
  53. <?= $tips ?>
  54. <?php elseif (count($imgs)) : ?>
  55. <!-- 预览当前图片 -->
  56. <?php foreach ($imgs as $img) :?>
  57. <img src="<?= $img?>" width="200">
  58. <?php endforeach;?>
  59. <?php endif; ?>
  60. </body>
  61. </html>

2. 多文件上传(批量)

  1. <?php
  2. $imgs = [];
  3. //遍历:实现单文件/多文件上传
  4. //判断是否上传
  5. if (!isset($_FILES['my_pic'])) {
  6. $tips = '没有文件上传';
  7. } else {
  8. foreach ($_FILES['my_pic']['error'] as $key => $error) {
  9. // error = 0 : 表示上传成功
  10. if ($error == 0) {
  11. //判断文件类型是否正确
  12. $file = $_FILES['my_pic'];
  13. //根据 error 的 key 来获取对应的文件名,类型,临时文件名等
  14. if (strstr($file['type'][$key], '/', true) != 'image') {
  15. $tips = '文件类型错误';
  16. continue;
  17. } else {
  18. //创建目标文件名,只要保存不重名
  19. $targetName = 'uploads/' . md5($file['name'][$key]) . strstr($file['name'][$key], '.');
  20. //将文件从临时目录移动到目标目录
  21. if (!move_uploaded_file($file['tmp_name'][$key], $targetName)) {
  22. $tips = '文件移动失败';
  23. } else {
  24. //将上传的目标文件名保存到一个数组,供当前页面进行预览
  25. $imgs[] = $targetName;
  26. }
  27. }
  28. }
  29. }
  30. }
  31. ?>
  32. <!DOCTYPE html>
  33. <html lang="en">
  34. <head>
  35. <meta charset="UTF-8">
  36. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  37. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  38. <title>文件批量上传</title>
  39. </head>
  40. <body>
  41. <form action="" method="post" enctype="multipart/form-data">
  42. <fieldset>
  43. <!--
  44. 多文件上传:
  45. 1.name为数组:my_pic[]
  46. 2.multiple 属性
  47. -->
  48. <legend>文件上传</legend>
  49. <input type="file" name="my_pic[]" multiple>
  50. <button>上传</button>
  51. </fieldset>
  52. </form>
  53. <!-- 判断是否有错误 -->
  54. <?php if (isset($tips)) : ?>
  55. <!-- 显示错误 -->
  56. <?= $tips ?>
  57. <?php elseif (count($imgs)) : ?>
  58. <!-- 预览当前图片 -->
  59. <?php foreach ($imgs as $img) : ?>
  60. <img src="<?= $img ?>" width="200">
  61. <?php endforeach; ?>
  62. <?php endif; ?>
  63. </body>
  64. </html>
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