isValid()) { $ext = $file->getUploadExtension(); if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'bmp', 'JPG', 'JPEG', 'PBG'])) { $type = 'images'; } else { $type = 'other'; } $fileSize = $file->getSize(); $uploadDir = '/files/' . $type . '/' . date('Ym') . '/'; $filename = date('YmdHis') . rand(999, 99999); $uploadPath = $uploadDir . $filename . '.' . $ext; $rootUploadDir = public_path() . $uploadDir; if (!is_dir($rootUploadDir)) { mkdir($rootUploadDir, 0777, true); } $filePath = public_path() . $uploadPath; $file->move($filePath); //上传到七牛云 $res = QiniuUtils::upload($filePath); if($res['code'] == ResponseCode::FAIL){ throw new Exception($res['msg']); } //删除本地图片 unlink($filePath); return [ 'code' => ResponseCode::SUCCESS, 'msg' => '上传成功', 'data' => $res['data'] ]; }else{ throw new Exception('文件无效'); } }catch (Exception $e){ return [ 'code' => ResponseCode::FAIL, 'msg' => $e->getMessage() ]; } } }