tp5怎么把片上传到七牛云上

Upload.php文件内容:下面

<?php

namespace app\common\controller;

use Qiniu\Auth as Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
use think\Controller;
use think\Request;


class Upload extends Controller
{
    /**
     * @return array|\think\response\Json
     * @throws \Exception
     */
    public function upload(Request $request)
    {


        $file = $request->file('file');

        if($file==null){
            return ["err"=>1,"msg"=>"上传文件为空" ,"data"=>""];
        }

        // 要上传图片的本地路径
        $filePath = $file->getRealPath();

        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);  //后缀

        // 上传到七牛后保存的文件名
        $key = strval(time()).strval(mt_rand(100000,999999)).'.'.$ext;
        require_once APP_PATH . '/../vendor/qiniu/autoload.php';
        // 需要填写你的 Access Key 和 Secret Key
        $accessKey = config('ACCESSKEY');
        $secretKey = config('SECRETKEY');
        // 构建鉴权对象
        $auth = new Auth($accessKey, $secretKey);
        // 要上传的空间
        $bucket = config('BUCKET');
        $domain = config('DOMAINImage');
        $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
            return json(["err"=>0,"msg"=>"上传完成","data"=>config("DOMAIN").$key]);
        }
    }


}

猜你喜欢

转载自blog.csdn.net/fzxyxf1314/article/details/89489433