登录  /  注册
博主信息
博文 94
粉丝 0
评论 0
访问量 89377
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
【PHP】PHP 文件上传
可乐随笔
原创
1895人浏览过

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>
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学