1
|
|
|
<?php |
2
|
|
|
namespace Qiniu\Storage; |
3
|
|
|
|
4
|
|
|
use Qiniu\Http\Client; |
5
|
|
|
use Qiniu\Http\Error; |
6
|
|
|
|
7
|
|
|
final class FormUploader |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* 上传二进制流到七牛, 内部使用 |
12
|
|
|
* |
13
|
|
|
* @param $upToken 上传凭证 |
14
|
|
|
* @param $key 上传文件名 |
15
|
|
|
* @param $data 上传二进制流 |
16
|
|
|
* @param $config 上传配置 |
17
|
|
|
* @param $params 自定义变量,规格参考 |
18
|
|
|
* http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar |
19
|
|
|
* @param $mime 上传数据的mimeType |
20
|
|
|
* |
21
|
|
|
* @return array 包含已上传文件的信息,类似: |
22
|
|
|
* [ |
23
|
|
|
* "hash" => "<Hash string>", |
24
|
|
|
* "key" => "<Key string>" |
25
|
|
|
* ] |
26
|
|
|
*/ |
27
|
6 |
|
public static function put( |
28
|
|
|
$upToken, |
29
|
|
|
$key, |
30
|
|
|
$data, |
31
|
|
|
$config, |
32
|
|
|
$params, |
33
|
|
|
$mime, |
34
|
|
|
$checkCrc, |
|
|
|
|
35
|
|
|
$filePath |
36
|
|
|
) { |
37
|
|
|
|
38
|
3 |
|
$fields = array('token' => $upToken); |
39
|
3 |
|
if ($key === null) { |
40
|
|
|
$fname = $filePath; |
41
|
|
|
} else { |
42
|
6 |
|
$fname = $filePath; |
43
|
3 |
|
$fields['key'] = $key; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
//enable crc32 check by default |
47
|
3 |
|
$fields['crc32'] = \Qiniu\crc32_data($data); |
48
|
|
|
|
49
|
3 |
|
if ($params) { |
50
|
|
|
foreach ($params as $k => $v) { |
51
|
|
|
$fields[$k] = $v; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
3 |
|
list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken); |
56
|
3 |
|
if ($err != null) { |
57
|
|
|
return array(null, $err); |
58
|
|
|
} |
59
|
|
|
|
60
|
3 |
|
$upHost = $config->getUpHost($accessKey, $bucket); |
61
|
|
|
|
62
|
|
|
$response = Client::multipartPost($upHost, $fields, 'file', $fname, $data, $mime); |
63
|
|
|
if (!$response->ok()) { |
64
|
|
|
return array(null, new Error($upHost, $response)); |
65
|
|
|
} |
66
|
|
|
return array($response->json(), null); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* 上传文件到七牛,内部使用 |
71
|
|
|
* |
72
|
|
|
* @param $upToken 上传凭证 |
73
|
|
|
* @param $key 上传文件名 |
74
|
|
|
* @param $filePath 上传文件的路径 |
75
|
|
|
* @param $config 上传配置 |
76
|
|
|
* @param $params 自定义变量,规格参考 |
77
|
|
|
* http://developer.qiniu.com/docs/v6/api/overview/up/response/vars.html#xvar |
78
|
|
|
* @param $mime 上传数据的mimeType |
79
|
|
|
* |
80
|
|
|
* @return array 包含已上传文件的信息,类似: |
81
|
|
|
* [ |
82
|
|
|
* "hash" => "<Hash string>", |
83
|
|
|
* "key" => "<Key string>" |
84
|
|
|
* ] |
85
|
|
|
*/ |
86
|
3 |
|
public static function putFile( |
87
|
|
|
$upToken, |
88
|
|
|
$key, |
89
|
|
|
$filePath, |
90
|
|
|
$config, |
91
|
|
|
$params, |
92
|
|
|
$mime |
93
|
|
|
) { |
94
|
|
|
|
95
|
|
|
|
96
|
3 |
|
$fields = array('token' => $upToken, 'file' => self::createFile($filePath, $mime)); |
97
|
3 |
|
if ($key !== null) { |
98
|
3 |
|
$fields['key'] = $key; |
99
|
3 |
|
} |
100
|
|
|
|
101
|
3 |
|
$fields['crc32'] = \Qiniu\crc32_file($filePath); |
102
|
|
|
|
103
|
3 |
|
if ($params) { |
104
|
|
|
foreach ($params as $k => $v) { |
105
|
|
|
$fields[$k] = $v; |
106
|
|
|
} |
107
|
|
|
} |
108
|
3 |
|
$fields['key'] = $key; |
109
|
3 |
|
$headers = array('Content-Type' => 'multipart/form-data'); |
110
|
|
|
|
111
|
3 |
|
list($accessKey, $bucket, $err) = \Qiniu\explodeUpToken($upToken); |
112
|
3 |
|
if ($err != null) { |
113
|
|
|
return array(null, $err); |
114
|
|
|
} |
115
|
|
|
|
116
|
3 |
|
$upHost = $config->getUpHost($accessKey, $bucket); |
117
|
|
|
|
118
|
|
|
$response = Client::post($upHost, $fields, $headers); |
119
|
|
|
if (!$response->ok()) { |
120
|
|
|
return array(null, new Error($upHost, $response)); |
121
|
|
|
} |
122
|
|
|
return array($response->json(), null); |
123
|
|
|
} |
124
|
|
|
|
125
|
3 |
|
private static function createFile($filename, $mime) |
126
|
|
|
{ |
127
|
|
|
// PHP 5.5 introduced a CurlFile object that deprecates the old @filename syntax |
128
|
|
|
// See: https://wiki.php.net/rfc/curl-file-upload |
129
|
3 |
|
if (function_exists('curl_file_create')) { |
130
|
2 |
|
return curl_file_create($filename, $mime); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// Use the old style if using an older version of PHP |
134
|
1 |
|
$value = "@{$filename}"; |
135
|
1 |
|
if (!empty($mime)) { |
136
|
1 |
|
$value .= ';type=' . $mime; |
137
|
1 |
|
} |
138
|
|
|
|
139
|
1 |
|
return $value; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.