Blogger Information
Blog 27
fans 0
comment 0
visits 17441
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
031-12月09日-PHP第21节-文件上传等
冇忉丼
Original
881 people have browsed it

写一个文件操作案例,要求前后端都要有

前端页面

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>文件操作</title>
  6. </head>
  7. <body>
  8. <h2>文件上传</h2>
  9. <form action="index.php" method="post" enctype="multipart/form-data">
  10. <input type="file" name="my_file">
  11. <!-- //name值-&#45;&#45;php全局变量获取的文件名-->
  12. <button style="color: #0C9A9A">上传</button>
  13. </form>
  14. </body>
  15. </html>

后端页面

  1. <?php
  2. if(!isset($_FILES['my_file'])){
  3. echo '<script>alert("请上传文件");</script>';
  4. }
  5. //01.配置上传参数
  6. $fileType = ['doc','docs','txt','pdf'];
  7. $fileSize = 10240000.;
  8. $filePath = '/uploads/';//前后都有斜杠
  9. $fileName = $_FILES['my_file']['name'];//原始文件名
  10. $tempFile = $_FILES['my_file']['tmp_name'];//临时文件名
  11. //02.判断上传情况
  12. $uploadError = $_FILES['my_file']['error'];
  13. if($uploadError>0){
  14. switch ($uploadError){
  15. case 1:
  16. case 2:die('文件太大');//为什么会1,2两种情况,实为一种情况
  17. case 3:die('文件格式有误');//为什么不加break
  18. default:die('未知错误');
  19. }
  20. }
  21. //03.文件扩展名是否支持
  22. $exten_name = explode('.',$fileName)[1];
  23. if(!in_array($exten_name,$fileType)){
  24. die('不允许上传' . $exten_name . '文件类型');
  25. }
  26. //04.生成不可重复的临时文件名--时间并加密处理
  27. $fileName = date('YmdHis',time()).md5(mt_rand(1,99)) . '.' .$exten_name;
  28. //05.文件上传
  29. if(is_uploaded_file($tempFile)){
  30. if(move_uploaded_file($tempFile,__DIR__ . $filePath . $fileName)){
  31. echo '上传成功';
  32. }else{
  33. die('上传失败');
  34. }
  35. }else{
  36. die('非法操作');
  37. }
  38. exit;

效果:


手抄

总结

1.容器的结构:

  1. class Container{
  2. private $instance;
  3. public function bind($instance,$paragm){
  4. }
  5. public function make($instance,$paragms=[]){
  6. }
  7. }

2.CSS—grid栅格布局将界面划分为网格,一般用在二维排布空间(弹性布局在一维布局方面更方便)
3.文件上传的5步:01.配置上传参数,02.判断上传情况,03.文件扩展名是否支持,04.生成不可重复的临时文件名—时间并加密处理,05.文件上传

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:这个作业与网格布局无关, grid布局留给大家自学
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