php七牛 批量上传,thinkphp 七牛存储单文件、多文件上传,简化配置

简化使用七牛云存储实现文件上传功能

/**

* 上传七牛

*

* @author huqitao 

*/

namespace Common\Utility;

class UploadImg {

protected $config;

protected $domain;

protected $bucket;

public function __construct($domain, $bucket) {

$this->domain = $domain;

$this->bucket = $bucket;

$this->config = array(

'maxSize' => 2 * 1024 * 1024, //文件大小

'rootPath' => './',

'saveName' => array('uniqid', ''),

'driver' => 'Qiniu',

'driverConfig' => array(

'secretKey' => '######',  //七牛空间配置参数

'accessKey' => '########',

'domain' => $this->domain, //空间地址

'bucket' => $this->bucket, //空间名称

)

);

}

/**

* 上传一个文件

* @param array $file 文件参数

* @return array 返回 code 与文件路径或错误信息

*/

public function uploadOne($file) {

$upload = new \Think\Upload($this->config);

$info = $upload->uploadOne($file);

if (!$info) {

return [550, $upload->getError()];

}

return [200, $info['url']];

}

/**

* 上传多图

* @param type $files

* @return array 返回 code 与文件路径数组 或错误信息

*/

public function uploads($files) {

$upload = new \Think\Upload($this->config);

$info = $upload->upload($files);

if (!$info) {

return [550, $upload->getError()];

}

foreach ($info as $v) {

$pArray[] = "http://".$this->domain."/".strtr($v['name'], '/', '_');

}

return [200, $pArray];

}

}使用方法,单图上传:public function uploadSchool() {

if (IS_POST) {

$domain = "#####";

$bucket = "###";

if (empty($_FILES)) {

$this->ajaxReturn(makeinformation(550, "没有上传图片"));

}

$uploadImg = new \Common\Utility\UploadImg($domain, $bucket);

$data = $uploadImg->uploadOne($_FILES['file']);

if ($data[0] != 200) {

$this->ajaxReturn(makeinformation(550, $data[1]));//失败返回错误

}

$this->ajaxReturn(makeinformation(200, '', array('url' => $data[1]))); //成功返回图片绝对地址

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值