hyperf oss上传

官方文档:hyperf 文件系统

安装

composer require hyperf/filesystem
composer require xxtime/flysystem-aliyun-oss
# 生成配置文件
php bin/hyperf.php vendor:publish hyperf/filesystem

 安装oss时报错

 

 修改composer.lock

将2.3.2 更改为1.0.49,再次执行composer

图片上传程序

use Hyperf\Utils\Str;
use Hyperf\Filesystem\FilesystemFactory;
use Hyperf\HttpServer\Contract\RequestInterface;

/**
 * 图片文件oss
 * @RequestMapping(path="upload_avatar_oss", methods="post")
 * @param RequestInterface $request
 * @param FilesystemFactory $factory
 * @return array
 * @throws \League\Flysystem\FileExistsException
 */
public function uploadAvatarOss(RequestInterface $request, FilesystemFactory $factory)
{
    $file_type = $request->input('file_type', 'uploads'); // 保存位置
    $file = $request->file('avatar');
    $oss = $factory->get('oss');
    $allowedArr = ['jpeg', 'gif', 'jpg', 'png'/*, 'mp3', 'mp4'*/];
    $ext = strtolower($file->getExtension());
    if (!in_array($ext, $allowedArr)) {
        throw new ServiceException(403, '上传格式不允许');
    }
    $folderPath = $file_type . '/' . date('Ymd') . '/' . Str::random(16) . '.' . $ext;

    $contents = file_get_contents($file->getRealPath());
    $res = $oss->write($folderPath, $contents);
    unset($contents);
    if($res == false) {
        return ['code' => ResponseCode::CODE_ERROR, 'msg' => "上传失败", 'data' => [$res]];
    }
    return ['code' => ResponseCode::CODE_SUCCESS, 'msg' => "上传成功", 'data' => [
        'full_path' => config('oss_document_url') . $folderPath],
        'path' => $folderPath,
    ];
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值