首先,请用composer下载七牛phpSDK (具体参考官方文档)

composer require qiniu/php-sdk

注册七牛账号 获取 AK SK(密匙) ,创建资源对象 获取domain(访问域名) bucket(你创建的资源对象名称)

models层定义AK SK domain bucket等常量 方便日后使用(因人而异)

const AK = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const SK = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const DOMAIN = 'xxxxxxx.com';
const BUCKET = 'xxxxxxxxxxx';

controller中加载七牛类

use crazyfdqiniuQiniu;

实例化七牛对象执行上传

//实例化七牛对象
$qiniu = new Qiniu(Product::AK,Product::SK,Product::DOMAIN,Product::BUCKET); //生成key(图片标志)
$key = uniqid(); //执行上传
$qiniu->uploadFile($_FILES['Product']['tmp_name']['cover'],$key); //获取存储地址
$cover = $qiniu->getLink($key); ------后面就是把路径存到数据库即可

多图片上传同理foreach即可

foreach($_FILES['Product 大专栏  yii2.0 集合七牛SDK 上传图片到第三方']['tmp_name']['pics'] as $k  => $file){
if($_FILES['Product']['error']['pics'][$k]>0){
continue;
}
$key = uniqid();
$qiniu->uploadFile($file,$key);
//(保存key为下标 方便后面删除)
$pics[$key] = $qiniu->getLink($key);
}

多图片集合返回json格式

值得一提的是七牛可以设置多图片样式,比如我就将图片设置成3种,product_small,product_big,product_mid;

view视图输出

----
七牛存储的图片删除 $productid = yii::$app->request->get('productid');
$product_del = Product::find()->where('productid=:pid',[':pid'=>$productid])->one();
//获取链接中文件名
$key = basename($product_del->cover);
//七牛
$qiniu = new Qiniu(Product::AK,Product::SK,Product::DOMAIN,Product::BUCKET); $qiniu->delete($key);
//删除多图片(因为保存时将文件名保存为下标值可直接删除)
$pics = json_decode($product_del->pics,true);
foreach ($pics as $key => $value) {
$qiniu->delete($key);
}

最新文章

  1. jQuery2.x源码解析(回调篇)
  2. Scrum Guide - Scrum指南中文版
  3. wgsim说明
  4. Unity 产生各不相同的随机数
  5. maven依赖本地非repository中的jar包-依赖jar包放在WEB-INF/lib等目录下的情况客户端编译出错的处理
  6. SqlServer查看各个表所占空间大小的sql
  7. Python进阶-面向对象
  8. [LeetCode] Maximal Rectangle(good)
  9. SQL远程创建数据库
  10. ABBYY PDF Transformer+从文件选项中创建PDF文档的教程
  11. 【WCF--初入江湖】03 配置服务
  12. C#学习笔记(四):委托和事件
  13. codeforces Codeforces 650A Watchmen
  14. sql(SqlServer)编程基本语法
  15. 匿名函数中undefined形参疑问(转载)
  16. 工厂方法配置jdbc连接
  17. 配置apache
  18. Spring Mobile——探测客户端设备和系统
  19. React简明学习
  20. SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)

热门文章

  1. 豆瓣爬虫Scrapy“抄袭”改写
  2. mysql 获取数据库和表结构信息
  3. 5314跳跃游戏IV
  4. 吴裕雄--天生自然 JAVA开发学习:接口
  5. Servlet&JSP复习笔记 02
  6. 关于前端CSS的总结
  7. Opencv笔记(十九)——直方图(一)
  8. 字符串常用方法总结与StringBuffer基础
  9. VirtualBox端口映射
  10. django rest framework 小小心得