码迷,mamicode.com
首页 > Web开发 > 详细

php七牛云

时间:2020-06-27 16:03:35      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:token   dmg   access   tmg   space   ora   qiniu   path   link   

<?php

namespace lib;

use think\Loader;

/**
* Description of Qrcode
*
* @author lsf
*/
class Qiniu {

private $bucket;
private $uploadMgr;
private $qiniuyunlink;
private $bucketMgr;

/**
* 初始化上传
* @param type $seeting=array("access_key"=>"七牛key","secret_key"=>"七牛secret","bucket"=>"七牛空间","qiniuyunlink"=>"七牛云域名")
*/
public function __construct($seeting) {
Loader::import("qiniu/autoload", EXTEND_PATH);
$auth = new \Qiniu\Auth($seeting["access_key"], $seeting["secret_key"]);
$this->qiniuyunlink = $seeting["qiniuyunlink"];
$this->bucket = $auth->uploadToken($seeting["bucket"]);
$this->uploadMgr = new \Qiniu\Storage\UploadManager();
$this->bucketMgr = new \Qiniu\Storage\BucketManager($auth);
}

/**
* 七牛云文件上传
* @param type $file
*/
public function upload($file) {
$filePath = $file[‘tmp_name‘];
$name = date("YmdHis", time()) . rand(100000, 999999);
if ($file[‘type‘] == ‘video/mp4‘) {
$key = ‘video‘ . $name . ‘.mp4‘;
} elseif ($file[‘type‘] == ‘audio/mp3‘||$file[‘type‘] == ‘audio/mpeg‘) {
$key = ‘audio‘ . $name . ‘.mp3‘;
} else {
$key = ‘png‘ . $name . ‘.png‘;
}
list($ret, $err) = $this->uploadMgr->putFile($this->bucket, $key, $filePath);
if ($err !== null) {
return array("success" => false, "msg" => "文件上传失败");
} else {
return array("success" => true, "msg" => "", "path" => $ret[‘key‘]);
}
}
/**
* 获取uploadToken
* @param type $link 直接是不带域名的文件名
*/
public function uploadToken() {
return $this->bucket;
}
/**
* 七牛云文件删除
* @param type $link 直接是不带域名的文件名
*/
public function fileDel($link) {
if (!empty($link)) {
$re = $this->bucketMgr->delete($this->bucket, $link);
if (empty($re)) {
return array("success" => true, "msg" => "", "data" => "");
} else {
return array("success" => false, "msg" => $re, "data" => "");
}
}
return array("success" => true, "msg" => "", "data" => "");
}

}

php七牛云

标签:token   dmg   access   tmg   space   ora   qiniu   path   link   

原文地址:https://www.cnblogs.com/-lsf/p/13198510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!