Blogger Information
Blog 34
fans 0
comment 0
visits 22405
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
12月9日—PHP文件上传
曾龙宇
Original
601 people have browsed it

前端文件

注意点:form表单中method必须是“post”,还要加上enctype=”multipart/form-data”属性。

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>图片上传</title>
  6. </head>
  7. <body>
  8. <form action="upload.php" method="post" enctype="multipart/form-data">
  9. <input type="file" name="my_file" id="">
  10. <button>上传</button>
  11. </form>
  12. </body>
  13. </html>

后端文件

文件上传用“move_uploaded_file”方法,放置上传文件的文件夹要提前创建好。

  1. <?php
  2. header('content-type:text/html;charset=utf-8');
  3. //配置上传参数
  4. $fileType = ['jpg','png','gif','jpeg'];//上传图片格式
  5. $fileSize = 3145728;//上传图片大小
  6. $filePath = '/uploads/';//放置图片的文件夹
  7. //原始文件名
  8. $fileName = $_FILES['my_file']['name'];
  9. //临时文件名
  10. $tempFile = $_FILES['my_file']['tmp_name'];
  11. //判断是否上传成功
  12. $uploadError = $_FILES['my_file']['error'];
  13. if ($uploadError>0){
  14. switch ($uploadError){
  15. case '1':
  16. case '2':
  17. die('上传文件过大!');
  18. case '3':
  19. die('上传文件不完整!');
  20. case '4':
  21. die('上传文件为空!');
  22. default:
  23. die('上传文件失败!');
  24. }
  25. }
  26. //判断文件扩展名是否支持
  27. $extension = explode('.',$fileName)[1];
  28. if (!in_array($extension,$fileType)){
  29. die('不允许上传'.$extension.'文件类型');
  30. }
  31. //生成不可重复的临时文件名
  32. $fileName = mt_rand(1,99).$fileName;
  33. //文件上传
  34. if (is_uploaded_file($tempFile)){
  35. if (move_uploaded_file($tempFile,__DIR__.$filePath.$fileName)){
  36. echo '上传图片成功';
  37. }else{
  38. die('上传图片失败');
  39. }
  40. }else{
  41. die('非法操作');
  42. }
  43. 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