php七牛云
Posted 打你一下
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php七牛云相关的知识,希望对你有一定的参考价值。
<?php
namespace lib;
use thinkLoader;
/**
* 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 QiniuAuth($seeting["access_key"], $seeting["secret_key"]);
$this->qiniuyunlink = $seeting["qiniuyunlink"];
$this->bucket = $auth->uploadToken($seeting["bucket"]);
$this->uploadMgr = new QiniuStorageUploadManager();
$this->bucketMgr = new QiniuStorageBucketManager($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七牛云的主要内容,如果未能解决你的问题,请参考以下文章