Blogger Information
Blog 35
fans 0
comment 0
visits 16726
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
多文件上传
手机用户311660634
Original
943 people have browsed it
  1. <?php
  2. use const ParagonIE\ConstantTime\true;
  3. // 遍历,单文件多文件上传
  4. $imgs = [];
  5. foreach($_FILES as $file){
  6. if($file['error'] == 0){
  7. // error等于0表示上传成功,这边判断一下,有上传成功在接着走
  8. // 判断一下文件类型是否争取,错误优先处理
  9. if(strstr($file['type'], '/', true) !=='image'){
  10. $tips = '文件类型错误';
  11. continue;
  12. }else{
  13. // 创建目标文件名,MD5加密
  14. $targetName = 'uploads/' . md5($file['name']) . strstr($file['name'],'.');
  15. // 将文件从临时目录移动都目标目录,重命名
  16. if(!move_uploaded_file($file['tmp_name'],$targetName)){
  17. $tips = '移动失败';
  18. }else{
  19. // 将上传的文件保存到一个数组中,进行预览
  20. $imgs[]= $targetName;
  21. }
  22. }
  23. }
  24. ?>
  25. <!DOCTYPE html>
  26. <html lang="en">
  27. <head>
  28. <meta charset="UTF-8">
  29. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  30. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  31. <title>Document</title>
  32. </head>
  33. <body>
  34. <!-- action为空,表示提交到当前表单,必须使用post方法, -->
  35. <form action="" method="post" enctype="multipart/form-data">
  36. <fieldset>
  37. <legend>文件上传</legend>
  38. <input type="file" name="pic">
  39. <input type="file" name="pic1">
  40. <input type="file" name="pic2">
  41. <button>上传</button>
  42. </fieldset>
  43. </form>
  44. <!-- 判断是否错误 - -->
  45. <?php if(isset($tips)) :?>
  46. <!-- 显示错误 -->
  47. <?php $tips ?>
  48. <!-- 如果没有错误,判断是否存在上传的图片 -->
  49. <?php elseif(count($imgs) > 0): ?>
  50. <!-- 预览当前图片 -->
  51. <?php foreach($imgs as $img):?>
  52. <img src="<?php $img ?>" alt="" width="300">
  53. <?php endforeach ?>
  54. <?php endif ?>
  55. </body>
  56. </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