0
点赞
收藏
分享

微信扫一扫

七牛云 php 代码和教材


大型图片,视频类网站,做流媒体等都可以用到

1、首先,域名绑定常见空间时生成的cdn域名

2、其次域名解析到cdn域名上

七牛云 php 代码和教材_php

3、把空间设置程公有

然后获取参数

七牛云 php 代码和教材_php_02


七牛云 php 代码和教材_上传图片_03


七牛云 php 代码和教材_上传_04

4、下面的代码

简单的

// 引入七牛鉴权类
use Qiniu\Auth;
// 引入七牛上传类
use Qiniu\Storage\UploadManager;
// 七牛云配置信息
$accessKey=Config::get("qiniu.accessKey");
$secretKey=Config::get("qiniu.secretKey");
$bucketName=Config::get("qiniu.bucket");
$bucketurl=Config::get("qiniu.bucketurl");
//默认file
$fileName=Config::get("qiniu.fileName");

$file = $this->request->file($fileName);
if (empty($file)) {
$this->error('请选择要上传的文件');
}

// 要上传图片的本地路径
$filePath = $file->getRealPath();
$ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION); //后缀
// 上传到七牛后保存的文件名
$key =substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;


// 构建鉴权对象
$auth = new Auth($accessKey, $secretKey);
$uptoken= $token = $auth->uploadToken($bucket);

$uploadMgr = new UploadManager();

list($ret, $err) = $uploadMgr->putFile($uptoken, $key, $filePath);
if ($err !== null) {
$this->error('上传失败');
} else {
$key = 'http://'.$bucketurl.'/'.$ret['key'];
$data['url'] = $key;
$this->success('上传成功',$data);
}

麻烦的

$accessKey=Config::get("qiniu.accessKey");
$secretKey=Config::get("qiniu.secretKey");
$bucketName=Config::get("qiniu.bucket");


$file = request()->file($_GET["name"]);
// 要上传图片的本地路径
$filePath = $file->getRealPath();
$ext = pathinfo($file->getInfo('name'), PATHINFO_EXTENSION); //后缀
// 上传到七牛后保存的文件名
$key =substr(md5($file->getRealPath()) , 0, 5). date('YmdHis') . rand(0, 9999) . '.' . $ext;
require_once APP_PATH . '../vendor/php-sdk-master/autoload.php';
// 需要填写你的 Access Key 和 Secret Key
$accessKey = Config::get('qiniu.accessKey');
$secretKey = Config::get('qiniu.secretKey');
// 构建鉴权对象
$auth = new Auth($accessKey, $secretKey);
// 要上传的空间
$bucket = Config::get('qiniu.bucket');
$domain = Config::get('qiniu.DOMAIN');

$uptoken= $token = $auth->uploadToken($bucket);

$uploadMgr = new UploadManager();

list($ret, $err) = $uploadMgr->putFile($uptoken, $key, $filePath);

if ($err !== null) {
$return = [
'code' => 0,
'msg' => '上传图片失败'
];
} else {
$key = $ret['key'];
$style = 'new';
$domain = 'http://q55jtsk1y.bkt.clouddn.com';

//baseUrl构造成私有空间的域名/key的形式
// 原图保护已开启(到七牛云控制面板查看样式分隔符 和 样式名称)
$baseUrl = $domain . '/' . $key . '-' . $style;
// 原图保护未开启
$baseUrl = $domain . '/' . $key;
$authUrl = $auth->privateDownloadUrl($baseUrl);

$return = [
'code' => 10000,
'msg' => '上传成功',
'data' => $authUrl// 'http://q55jtsk1y.bkt.clouddn.com//' . $ret['key']
];
}
return json_encode($return);


举报

相关推荐

0 条评论