Blogger Information
Blog 34
fans 0
comment 0
visits 20280
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php灭绝手把手教你玩文件上传
小庄
Original
552 people have browsed it

php灭绝手把手教你玩文件上传

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>文件上传</title>
  8. </head>
  9. <body>
  10. <form action="upload2.php" method="post" enctype="multipart/form-data">
  11. <fieldset>
  12. <legend>
  13. 单文件上传
  14. </legend>
  15. <label for="my_file"></label>
  16. <input type="file" name="my_file" id="my_file">
  17. <button>上传</button>
  18. </fieldset>
  19. </form>
  20. </body>
  21. </html>
  1. <?php
  2. echo '<pre>';
  3. var_dump(uploadFile($_FILES['my_file']));
  4. function uploadFile($fileInfo,$uploadPath='./uploads',$flag=true,$allowExt=['jpg','png','wbmp','gif','jpeg','bmp'],$maxSize=2097152){
  5. if($fileInfo['error'] == 0){
  6. $arr = explode('.',$fileInfo['name']);//explode 分割字符串,以(.)点,分割
  7. // var_dump($arr);
  8. $ext = end($arr); //取后缀
  9. $prefix = array_shift($arr); //取文件名
  10. if(!in_array($ext,$allowExt)){//判断后缀是否存在
  11. return $res = '文件类型不合法';
  12. }
  13. if($fileInfo['size'] > $maxSize){ //判断文件大小
  14. return $res = '文件大小超过限制的最大值';
  15. }
  16. if(!getimageSize($fileInfo['tmp_name'])){//检测图片是否合法
  17. return $res = '不是真实图片';
  18. }
  19. if(!is_uploaded_file($fileInfo['tmp_name'])){//检测上传方式是否为http post
  20. return $res = '上传方式错误:请使用http post 方式上传';
  21. }
  22. // $uploadPath = 'uploads/'; //存储文件目录
  23. if(!file_exists($uploadPath)){ //file_exists判断目录是否存在
  24. mkdir($uploadPath,0770,true); //mkdir创建目录,参数0770为权限
  25. chmod($uploadPath,0770); //为以防万一,再次修改权限为0770
  26. }
  27. // move_uploaded_file,copy 函数都可以移动文件
  28. // echo $prefix.time(); //调试用
  29. $des = $uploadPath.'/'.md5($prefix.time()).'.'.$ext; //将散列后的文件名,与后缀拼接。($prefix.time()拼接为原文件名+时间戳,md5 在将其散列形成独一无二的文件名,避免用户上传相同文件名的文件导致出现问题)
  30. // echo $des;//调试用
  31. $moveResult = move_uploaded_file($fileInfo['tmp_name'],$des);//参数:将$fileInfo['tmp_name']临时文件,复制到$des,
  32. if(!$moveResult){
  33. $res['error'] = '文件移动失败';
  34. }else{
  35. $res['info'] = $fileInfo['name'] . '上传成功';
  36. $res['fileRealPath'] = $des;
  37. }
  38. return $res;
  39. }else{
  40. switch($fileInfo['error']):
  41. case 1:
  42. echo '上传的文件超过了 php.ini 中 upload_max_filesize 选项限制的值';
  43. break;
  44. case 2:
  45. echo '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值';
  46. break;
  47. case 3:
  48. echo '文件只有部分被上传';
  49. break;
  50. case 4:
  51. echo '没有文件被上传。 是指表单的file域没有内容,是空字符串';
  52. break;
  53. case 6:
  54. echo '找不到临时文件夹';
  55. break;
  56. default:
  57. echo '系统错误';
  58. endswitch;
  59. }
  60. }
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!