QiniuUpload.class.php
4.32 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* 七牛上传
*
*/
class Lib_Utils_QiniuUpload implements Util_Upload_QiniuInterface
{
/**
* 文件上传
*
* param array array('file'=>'文件数组','prefix'=>'前缀’,'filePath'=>'文件存储路径')
* @return array(relaPath=>'')
*/
public static function fileUpload($params)
{
$ret = array();
if(isset($params['filePath']) && isset($params['file']) && isset($params['prefix']))
{
$filePath = $params['filePath'];
$file = $params['file'];
$prefix = $params['prefix'];
$bucket = self::getBucket($filePath, $prefix);
$project = self::getProject($filePath);
$key = self::getFileKey($file, $prefix);
$uploadKey = $key;
if(empty($project))//项目为空,去掉前面的/
{
$uploadKey = substr($uploadKey, 1);
}
$fileName = $file['tmp_name'];
Util_Qiniu_Upload::initKey(QINIU_UPLOAD_ACCESSKEY, QINIU_UPLOAD_SECRETKEY);
if(Util_Qiniu_Upload::upload($bucket, $project.$uploadKey, $fileName))
{
$ret = array('relaPath'=> $key);
}
}
return $ret;
}
/**
* 文件批量上传
*
* @param array ('file'=>'多个文件数组','prefix'=>'前缀’,'filePath'=>'文件存储路径')
* @return array(array(relaPath=>''))
*/
public static function fileBatchUpload($params)
{
$ret = array();
if(isset($params['filePath']) && isset($params['file']) && isset($params['prefix']))
{
$filePath = $params['filePath'];
$prefix = $params['prefix'];
Util_Qiniu_Upload::initKey(QINIU_UPLOAD_ACCESSKEY, QINIU_UPLOAD_SECRETKEY);
foreach($params['file'] as $fileKey => $file)
{
$bucket = self::getBucket($filePath, $prefix);
$key = self::getFileKey($file, $prefix);
$fileName = $file['tmp_name'];
$project = self::getProject($filePath);
$uploadKey = $key;
if(empty($project))//项目为空,去掉前面的/
{
$uploadKey = substr($uploadKey, 1);
}
if(Util_Qiniu_Upload::upload($bucket, $project.$uploadKey, $fileName))
{
$ret[$fileKey]['relaPath'] = $key;
}
}
}
return $ret;
}
/**
* 文件流上传
*
* @param string ('file'=>'文件流','prefix'=>'前缀’,'filePath'=>'文件存储路径')
* @return string
*/
public static function streamUpload($params)
{
$ret = '';
if(isset($params['filePath']) && isset($params['file']) && isset($params['prefix']))
{
$filePath = $params['filePath'];
$prefix = $params['prefix'];
$file = $params['file'];
Util_Qiniu_Upload::initKey(QINIU_UPLOAD_ACCESSKEY, QINIU_UPLOAD_SECRETKEY);
$bucket = self::getBucket($filePath, $prefix);
$key = self::getStreamKey($prefix);
$project = self::getProject($filePath);
$uploadKey = $key;
if(empty($project))//项目为空,去掉前面的/
{
$uploadKey = substr($uploadKey, 1);
}
if(Util_Qiniu_Upload::uploadStream($bucket, $project.$uploadKey, $file))
{
$ret = $key;
}
}
return $ret;
}
/**
* 获取空间
*
* @param string $filePath
* @param string $prefix
* @return string
*/
private static function getBucket($filePath, $prefix = 0)
{
//video暂时存储到show的视频上面
$buckets = array('video'=> array('showvideo'),'voice'=> array('wwwyohocnvoice'), 'img'=> array( 1 => 'wwwyohocnimg01', 2 => 'wwwyohocnimg02'));
foreach($buckets as $key => $bucket)
{
$pos = strrpos($filePath, $key);
$length = strlen($filePath);
if($pos !== false && $pos >= ($length - strlen($key) - 1))
{
return $bucket[intval($prefix)];
}
}
}
/**
* 获取模块
*
* @param string $filePath
* @return string
*/
private static function getProject($filePath)
{
if(strrpos($filePath, 'video') !== false || strrpos($filePath, 'voice') !== false)
{
return '';
}
else
{
return basename($filePath);
}
}
/**
* 获取文件方式key
*
* @param string $file
* @param string $prefix
* @return string
*/
private static function getFileKey($file, $prefix)
{
$fileInfo = pathinfo($file['name']);
$datePath = date('/Y/m/d/H/');
return $datePath.$prefix.md5(date('H') . $file['name'] . $file['type'] . $file['size'] ) . '.' . strtolower($fileInfo['extension']);
}
/**
* 获取文件流方式key
*
* @param string $prefix
* @return string
*/
private static function getStreamKey($prefix)
{
$datePath = date('/Y/m/d/H/');
return $datePath.$prefix.md5(microtime()) . '.jpg'; // 文件名以时间md5值命名
}
}