Blogger Information
Blog 33
fans 0
comment 0
visits 19824
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月9日作业文件上传--php培训九期线上班
取个名字真难
Original
579 people have browsed it

html文件

  1. <!doctype html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport"
  6. content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  7. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  8. <title>文件上传</title>
  9. </head>
  10. <body>
  11. <div>
  12. <form action="fileup.php" method="post" enctype="multipart/form-data">
  13. <label for="myfile">请上传图片:</label>
  14. <input type="file" name="myfile" id="myfile">
  15. <button>提交</button>
  16. </form>
  17. </div>
  18. </body>
  19. </html>

fileup.php

  1. <?php
  2. // 1. 配置上传参数
  3. $fileType=['jpg','png','gif'];
  4. $fileSize=3145728;
  5. $filePath='/mvc/';
  6. //得到上传文件名
  7. $fileName=$_FILES['myfile']['name'];
  8. //给上传文件一个临时文件名
  9. $tempFile=$_FILES['myfile']['tmp_name'];
  10. //2. 判断是否上传成功
  11. $uploadError=$_FILES['myfile']['error'];
  12. if($uploadError>0){
  13. switch ($uploadError){
  14. case 1:
  15. case 2:die('文件上传过大');
  16. case 3:die('文件上传不完整');
  17. default :die('未知');
  18. }
  19. }
  20. // 3. 判断文件上传类型?
  21. $extension = explode('.', $fileName)[1];
  22. if (!in_array($extension, $fileType)){
  23. die('请上传格式为:jpg、png.gif');
  24. }
  25. //生成临时文件名
  26. $fileName= date('YmdHis',time()).md5(mt_rand(1,99)).'.'.$extension;
  27. //文件上传
  28. if (is_uploaded_file($tempFile)){
  29. if (move_uploaded_file($tempFile,__DIR__.$filePath.$fileName)) {
  30. echo '上传成功';
  31. }else {
  32. echo '上传失败';
  33. }
  34. }else {
  35. die('非法操作');
  36. }
  37. exit;

总结

文件上传还是比较简单就是其中的几个函数还是得好好练习一下不然记不住

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