ThinkPHP5整合系列之调用七牛云SDK上传图片文件

浏览:2235 发布日期:2018/10/25 分类:业务逻辑 关键字: ThinkPHP5 七牛云
ThinkPHP5整合系列之调用七牛云SDK上传图片文件
在使用 SDK 之前,需要注册一个七牛帐号,然后创建空间,绑定自定义域名,并在控制台获取一对有效的AccessKey和SecretKey,并且详细阅读安全机制以进一步了解如何正确使用和管理密钥。

以下是PHP代码,可以根据你的项目自行修改<?php
namespace app\admin\controller;
use think\Controller;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
class Upload extends Controller
{
    function upload()
    {
        $file = request()->file('file');
        $path = $file->getRealPath();
        $ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
        $key = date('Ymd') . '/' . str_replace('.', '0', microtime(1)) . '.' . $ext;
        $ym = '这里填写你的域名';
        include '/extend/Qiniu/autoload.php';
        $accessKey = '这里填写你的AK';
        $secretKey = '这里填写你的SK';
        $auth = new Auth($accessKey, $secretKey);
        $bucket = '这里填写你的空间名称';
        $token = $auth->uploadToken($bucket);
        $upload = new UploadManager();
        list($ret, $err) = $upload->putFile($token, $key, $path);
        if (!$err) {
            return json(array('err'=>0,'msg'=>'上传成功','path'=>$ym . '/' . $ret['key']));
        }
    }
}
原文和SDK地址:https://www.featwork.com/blog/2-1-11.html
评论( 相关
后面还有条评论,点击查看>>