Blogger Information
Blog 43
fans 0
comment 0
visits 30581
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php上传函数封装
橙絮圆
Original
545 people have browsed it

PHP文件上传函数封装

作业标题:PHP文件上传函数封装
作业内容:根据需求封装文件上传限制函数: 1. 文件大小不超过5M 2. 后缀只能是[‘jpg’,’jpeg’,’png’,’wbmp’,’gif’] 3. 检查图片合法性 4.为每一张图片处理散列名称 5. 目标目录不存在,创建目录 6.设置错误日志接收系统错误,而不是将错误显示到页面上

  • 首页demo1.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. <!-- application/x-www-urlencoded 不合适二进制数据或者包含ASCII字符的传输 -->
    11. <form action="upload3.php" method="post" enctype="multipart/form-data">
    12. <!-- <fieldset>
    13. <legend>
    14. 单文件上传
    15. </legend>
    16. <label for="my_file"></label>
    17. <input type="file" name="my_file" id="my_file">
    18. <button>上传</button>
    19. </fieldset> -->
    20. <fieldset>
    21. <legend>
    22. 多文件上传
    23. </legend>
    24. <label for="my_file"></label>
    25. <input type="file" name="my_file[]" id="my_file" multiple>
    26. <button>上传</button>
    27. </fieldset>
    28. </form>
    29. </body>
    30. </html>
  • upload代码
  1. <?php
  2. ini_set('display_errors','Off'); //页面不现实任何错误
  3. ini_set('error_log','./error.log');
  4. require 'common1.php';
  5. $files = upload($_FILES['my_file']);
  6. foreach ($files as $fileInfo)
  7. {
  8. $res = uploadFile($fileInfo);
  9. echo "<p>{$res['info']}</p>";
  10. echo "<img style='border-radius:50%;width:100px';height:50% src='{$res['fileRealPath']}'>";
  11. $uploadFiles[] = $res['fileRealPath'];
  12. }
  13. printf('<pre>%s</pre>',print_r($uploadFiles,true));
  14. echo json_encode($uploadFiles);
  • 封装函数common.php
  1. <?php
  2. ini_set('display_errors','Off'); //页面不现实任何错误
  3. ini_set('error_log','./error.log');
  4. //文件上传函数
  5. function uploadFile($fileInfo,$uploadPath='./storage',$flag=true,$allowExt=['jpg','png','wbmp','gif','jpeg'],$maxSize=5242880)
  6. {
  7. if($fileInfo['error'] == 0)
  8. {
  9. // 后端设置文件的类型
  10. $arr = explode('.',$fileInfo['name']);
  11. $ext = end($arr);
  12. $prefix = array_shift($arr);
  13. if(!in_array($ext,$allowExt))
  14. {
  15. $res['error']='非法的文件类型';
  16. }
  17. if($fileInfo['size'] > $maxSize)
  18. {
  19. $res['error']='文件大小超过限制的最大值';
  20. }
  21. if($flag)
  22. {
  23. //检测图片是否合法
  24. if(!getimageSize($fileInfo['tmp_name']))
  25. {
  26. $res['error']='不是真实图片,get out~';
  27. }
  28. }
  29. if(!is_uploaded_file($fileInfo['tmp_name']))
  30. {
  31. $res['error'] ='上传方式错误:请使用http post方式上传';
  32. }
  33. if(!empty($res)) return;
  34. // -----------------------------------------------------------------------
  35. // copy(),move_uploaded_file()可以将文件从临时目录移动到指定路径
  36. if(!file_exists($uploadPath))
  37. {
  38. mkdir($uploadPath,0770,true);
  39. chmod($uploadPath,0770);
  40. }
  41. // 根据$prefix time() 生成唯一图片地址
  42. $des = $uploadPath.'/'.md5($prefix.time()).'.'.$ext;
  43. $result = move_uploaded_file($fileInfo['tmp_name'],$des);
  44. if(!$result)
  45. {
  46. $res['error'] = '文件移动失败';
  47. }else{
  48. $res['info'] = $fileInfo['name'] . '上传成功';
  49. $res['fileRealPath'] = $des;
  50. }
  51. return $res;
  52. }else{
  53. switch($error) :
  54. case 1:
  55. $res['error'] = '文件超过`php.ini`中`upload_max_filesize`值<br>';
  56. break;
  57. case 2:
  58. $res['error'] = '文件大小超过表单中`MAX_FILE_SIZE`指定的值<br>';
  59. break;
  60. case 3:
  61. $res['error'] = '文件只有部分被上传<br>';
  62. break;
  63. case 4:
  64. $res['error'] = '没有文件被上传<br>';
  65. break;
  66. case 6:
  67. $res['error'] = '找不到临时文件夹<br>';
  68. break;
  69. case 6:
  70. $res['error'] = '文件写入失败<br>';
  71. break;
  72. default:
  73. $res['error'] = '系统错误<br>';
  74. break;
  75. endswitch;
  76. return $res;
  77. }
  78. }
  79. function upload($fileInfo)
  80. {
  81. foreach($fileInfo as $k=>$v)
  82. {
  83. foreach($v as $kk=>$vv)
  84. {
  85. if($kk == $kk )
  86. {
  87. $files[$kk][$k] = $vv;
  88. }
  89. }
  90. }
  91. return $files;
  92. }
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