当前位置: 代码迷 >> 综合 >> todo-php-file-upload
  详细解决方案

todo-php-file-upload

热度:95   发布时间:2023-12-16 18:25:31.0
 /*** 上传文件(不能使用ThinkPHP提供的文件上传类,过滤了通过formdata上传的文件)** @param $file*/public function uploadFile($fileinfo){// 获取文件上传配置$cfg = config('upload_options');$fileName = $fileinfo['name'];// 判断文件后缀名$fileExt = pathinfo($fileName, PATHINFO_EXTENSION);if (!in_array($fileExt, $cfg['allow_type'])) {return ['status' => false, 'message' => '不允许该类型的文件'];}// 判断文件大小if ($fileinfo['size'] > $cfg['max_size']) {return ['status' => false, 'message' => '文件过大'];}// 判断是否是通过http请求上传的文件if (is_uploaded_file($fileName)) {return ['status' => false, 'message' => '非法上传'];}// 按天生成文件夹, 生成随机文件名$cfgPath = trim($cfg['save_path'], '/').'/'.date('Ymd');$savePath = rtrim(str_replace('\\', '/', ROOT_PATH), '/').'/public/'.$cfgPath;if (!file_exists($savePath)) {mkdir($savePath, 0777);}// 上传文件$fileName = md5(time().mt_rand(0, 9999)).'.'.$fileExt;if (move_uploaded_file($fileinfo['tmp_name'], $savePath.'/'.$fileName)) {return ['status' => true, 'message' => '上传成功', 'path' => $cfgPath.'/'.$fileName];} else {return ['status' => false, 'message' => '上传失败'];}}
  相关解决方案