Qiniu.class.php
3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2014 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: yangweijie <yangweijiester@gmail.com> <http://www.code-tech.diandian.com>
// +----------------------------------------------------------------------
namespace Think\Upload\Driver;
use Think\Upload\Driver\Qiniu\QiniuStorage;
class Qiniu
{
/**
* 上传文件根目录
*
* @var string
*/
private $rootPath;
/**
* 上传错误信息
*
* @var string
*/
private $error = '';
private $config = array(
'secrectKey' => '', // 七牛服务器
'accessKey' => '', // 七牛用户
'domain' => '', // 七牛密码
'bucket' => '', // 空间名称
'timeout' => 300
); // 超时时间
/**
* 构造函数,用于设置上传根路径
*
* @param array $config
* FTP配置
*/
public function __construct($config)
{
$this->config = array_merge($this->config, $config);
/* 设置根目录 */
$this->qiniu = new QiniuStorage($config);
}
/**
* 检测上传根目录(七牛上传时支持自动创建目录,直接返回)
*
* @param string $rootpath
* 根目录
* @return boolean true-检测通过,false-检测失败
*/
public function checkRootPath($rootpath)
{
$this->rootPath = trim($rootpath, './') . '/';
return true;
}
/**
* 检测上传目录(七牛上传时支持自动创建目录,直接返回)
*
* @param string $savepath
* 上传目录
* @return boolean 检测结果,true-通过,false-失败
*/
public function checkSavePath($savepath)
{
return true;
}
/**
* 创建文件夹 (七牛上传时支持自动创建目录,直接返回)
*
* @param string $savepath
* 目录名称
* @return boolean true-创建成功,false-创建失败
*/
public function mkdir($savepath)
{
return true;
}
/**
* 保存指定文件
*
* @param array $file
* 保存的文件信息
* @param boolean $replace
* 同名文件是否覆盖
* @return boolean 保存状态,true-成功,false-失败
*/
public function save(&$file, $replace = true)
{
$file['name'] = $file['savepath'] . $file['savename'];
$key = str_replace('/', '_', $file['name']);
$upfile = array(
'name' => 'file',
'fileName' => $key,
'fileBody' => file_get_contents($file['tmp_name'])
);
$config = array();
$result = $this->qiniu->upload($config, $upfile);
$url = $this->qiniu->downlink($key);
$file['url'] = $url;
return false === $result ? false : true;
}
/**
* 获取最后一次上传错误信息
*
* @return string 错误信息
*/
public function getError()
{
return $this->qiniu->errorStr;
}
}