thinkphp5调用七牛云SDK上传文件的详细步骤
今天需要将图片上传到七牛云上,在网上搜了些教程,都不怎么全,有的还缺东西,所以我把自己弄成功的步骤写出来,希望能帮到其他人,这个文章只适合thinkphp5,其他版本的请参考其他文档,文档中需要的七牛云PHP-SDK,请在七牛云官方下载,或者点我的文件床下载也可以,地址是http://filebed.rain1024.com/
1.先在verdor里新建一个文件夹: Qiniu
2.将SDK压缩包中的文件全部解压到Qiniu文件夹中
3.在Controller中,引用类
vendor('Qiniu.autoload');
use Qiniu\Auth as Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
4.在config.php中添加配置
'ACCESSKEY' => '',
'SECRETKEY' => '',
'BUCKET' => '',
'DOMAIN'=>'',
5.上传操作的函数
$file = request()->file('image');
$filePath = $file->getRealPath();
$ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION);
$controllerName = 'index';
$key =substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
$accessKey = config('ACCESSKEY');
$secretKey = config('SECRETKEY');
$auth = new Auth($accessKey, $secretKey);
$bucket = config('BUCKET');
$domain = config('DOMAINImage');
$token = $auth->uploadToken($bucket);
$uploadMgr = new UploadManager();
list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
if ($err !== null) {
echo ["err"=>1,"msg"=>$err,"data"=>""];
} else {
var_dump($ret);
}