Blogger Information
Blog 28
fans 0
comment 0
visits 21988
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php:文件上传
暝皑祯π_π
Original
993 people have browsed it

前端代码

  1. <?php
  2. require 'uploads.php';
  3. (new Uploads)->upload('pic', 'uplosd');
  4. ?>
  5. <!DOCTYPE html>
  6. <html lang="en">
  7. <head>
  8. <meta charset="UTF-8">
  9. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  10. <title>文件上传 </title>
  11. </head>
  12. <body>
  13. <form action="" method="POST" enctype="multipart/form-data">
  14. <fieldset>
  15. <legend>文件上传</legend>
  16. <input type="hidden" name="MAX_FILES_SIZE" value="500000">
  17. <input type="file" name="pic[]" id="file" multiple>
  18. <button>上传</button>
  19. </fieldset>
  20. </form>
  21. </body>
  22. </html>

Uploads.php

  1. <?php
  2. class Uploads
  3. {
  4. protected $filesname;
  5. public function upload($name, $past){
  6. $this->filesname = $name;
  7. // 判断上传的文件是否成功
  8. if($_FILES[ $this->filesname]){
  9. foreach($_FILES[$this->filesname]['error'] as $key=>$value){
  10. if($value === 0){
  11. // 临时文件名
  12. $tmpFileName = $_FILES[$this->filesname]['tmp_name'][$key];
  13. // 原始文件名
  14. $originalFileName =$_FILES[$this->filesname]['name'][$key];
  15. // 目标文件名
  16. $destFileName = $past.md5($originalFileName);
  17. // 移动文件:第一个参数:要移动的文件。第二个参数:目标文件名
  18. move_uploaded_file($tmpFileName,$destFileName);
  19. // 预览
  20. echo "<img src='{$destFileName}' width='100'>";
  21. echo "文件上传成功";
  22. }else{
  23. echo '文件上传失败';
  24. }
  25. }
  26. }
  27. }
  28. }
  29. ?>

效果图

总结

  • 第一次尝试封装类,感觉类的封装增加了程序的简洁、清晰,实现了代码的复用。
  • 文件上传时前端form必须是POST方式上传,method=”POST” enctype=”multipart/form-data”,而且表单发送前的编码方式必须是二进制编码。
Correcting teacher:天蓬老师天蓬老师

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