tp5中七牛云使用案例系列笔记(个人笔记)

1,百度编辑器中使用七牛云进行上传案例,根据网上案例进行接入tp5,经过小修改后实测没问题

目录如图

配置如下图

资源下载:https://download.csdn.net/download/itxiaolong3/15533263

2,接口使用七牛云案例,网上去下载七牛云php版的sdk放到extend目录下,命名为qiniu

第一步:在config中配置号七牛云信息

第二步:方法中使用

public function file_qiniu(){
        $file = request()->file('file');
        // 要上传图片的本地路径
        $filePath = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀
        // 上传到七牛后保存的文件名
        $key =date('YmdHis') . rand(0, 9999) . '.' . $ext;
        // 需要填写你的 Access Key 和 Secret Key
        // 构建鉴权对象
        $accessKey =config("qiniu")["accessKey"];
        $secretKey =config("qiniu")["secretKey"];
        $auth=new Auth($accessKey,$secretKey);
        // 要上传的空间
        $bucket =config("qiniu")["bucket"];
        //域名
        $domain=config("qiniu")["domain"];
        $token = $auth->uploadToken($bucket);
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
        if ($err !== null) {
            return ["err"=>1,"msg"=>$err,"data"=>""];
        } else {
            //返回图片的完整URL
            $imgPath='http://'.$domain.'/'.$key;
            //赋值
            $data["thumb_url"] = $imgPath;
            return json(["code"=> 1, "msg" => "上传成功", "url" =>$imgPath]);
        }
    }

//删除七牛云图片
    function delfromqiniu() {
        $src = str_replace(ROOT_PATH . '/', '', str_replace('//', '/', input('src')));
        $imginfo=explode('/',$src);
        $delFileName=$imginfo[count($imginfo)-1];
        if( $delFileName ==null){
            return json(["code"=>0,"msg"=>"删除失败,参数不正确",'url'=>'|'.input('src')]);
        }
        // 构建鉴权对象
        $accessKey =config("qiniu")["accessKey"];
        $secretKey =config("qiniu")["secretKey"];
        $auth=new Auth($accessKey,$secretKey);
        // 要上传的空间
        $bucket =config("qiniu")["bucket"];
        // 配置
        $config = new \Qiniu\Config();
        // 管理资源
        $bucketManager=new BucketManager($auth, $config);
        // 删除文件操作
        $res = $bucketManager->delete(config("qiniu.bucket"), $delFileName);
        if (is_null($res)) {
            return json(["code"=>1,"msg"=>"删除成功",'url'=>'']);
        }else{
            return json(["code"=>0,"msg"=>"删除失败,图片找不到",'url'=>'']);
        }

    }

如果报错Class 'Qiniu\Auth' not found

在tp5入口文件index.php中加入自动引入文件,如下图

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

itxiaolong3

打赏可以,但别打我就行

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值