PHP函数封装之文件上传

  • A+
所属分类:PHP

在很多时候,都觉得上传是一个很简单的工作,当你去正经去撸代码的时候,发现要考虑不少东西,如下出现的很多种可能,且如下代码在测试的时候一直没有通过,先记录下来,等到后期需要用到的时候再来研究。

上传函数

  1. function upload_file(array $fileInfo,string $uploadPath='./uploads',$imageFlag=true,$allowExt=array('jpeg','jpg','png','gif'),$maxSize=2097152){
  2.     define('UPLOAD_ERRS',[
  3.         'upload_max_filesize' =>'超过了PHP配置文件中upload_max_filesize选项的值',
  4.         'form_max_size' =>'超过了表单MAX_FILE_SIZE选项的值(注:前端可以绕过)',
  5.         'upload_file_partial' =>'文件部分被上传',
  6.         'no_upload_file_select'=>'没有选择上传文件',
  7.         'upload_system_error' =>'系统错误',
  8.         'no_allow_exut' =>'非法文件类型',
  9.         'exceed_max_size' =>'超出允许上传的最大值',
  10.         'not_true_image' => '文件不是真实图片',
  11.         'not_http_post'=>'文件不是通过HTTP POST方式上传上来的',
  12.         'move_error' => '移动失败',
  13.     ]);
  14.     //检测是否上传是否有错误
  15.     if ($fileInfo['error']===UPLOAD_ERR_OK){
  16.         //检测上传文件类型
  17.         $ext = strtolower(pathinfo($fileInfo['name'],PATHINFO_EXTENSION));
  18.         //检测文件的扩展名是否在允许的范围内
  19.         if (!in_array($ext,$allowExt)){
  20.             return UPLOAD_ERRS['no_allow_exut'];
  21.         }
  22.         //检测文件的大小
  23.         if ($fileInfo['size'] >$maxSize){
  24.             return UPLOAD_ERRS['form_max_size'];
  25.         }
  26.         //检测是否是真是图片
  27.         if ($imageFlag){
  28.             if (@!getimagesize($fileInfo['tmp_name'])){
  29.                 return UPLOAD_ERRS['not_true_image'];
  30.             }
  31.         }
  32.         //检测文件是否通过HTTP POST方式上传上来的
  33.         if (!is_uploaded_file($fileInfo['tmp_name'])){
  34.             return UPLOAD_ERRS['not_http_post'];
  35.         }
  36.         //检测目录是否存在,不存在则创建
  37.         if (!is_dir($uploadPath)){
  38.             mkdir($uploadPath,0777,true);
  39.         }
  40.         //生成唯一文件名,防止重名产生覆盖
  41.         $uniName = md5(uniqid(microtime(true),true)).'.'.$ext;
  42.         $dest =$uploadPath.DIRECTORY_SEPARATOR.$uniName;
  43.         //移动文件
  44.         if (@!move_uploaded_file($fileInfo['tmp_name'],$dest)){
  45.             return UPLOAD_ERRS['move_error'];
  46.         }
  47.         return $dest;
  48.     }else{
  49.         switch ($fileInfo['error']){
  50.             case 1:
  51.                 $mes =UPLOAD_ERRS['upload_max_filesize'];
  52.                 break;
  53.             case 2:
  54.                 $mes =UPLOAD_ERRS['form_max_size'];
  55.                 break;
  56.             case 3:
  57.                 $mes =UPLOAD_ERRS['upload_file_partial'];
  58.                 break;
  59.             case 4:
  60.                 $mes =UPLOAD_ERRS['no_upload_file_select'];
  61.                 break;
  62.             case 6:
  63.             case 7:
  64.                 $mes =UPLOAD_ERRS['upload_system_error'];
  65.                 break;
  66.         }
  67.         return $mes;
  68.     }
  69. }

扩展阅读:

PHP函数封装之文件上传 PHP函数封装之文件上传

李金龙

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: