七牛云整合Ueditor的ThinkPHP版本
首先去七牛云官网下载phpSDK工具放在Think/library/Vendor下。
ueditor后台调用方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/** * 上传文件 * @param */ public function UploadSomething(){ header( "Content-Type: text/html; charset=utf-8" ); error_reporting (E_ERROR); // 登录检测 if ( $this ->uid == 0){ if ( $_GET [ 'action' ] == 'config' ){ echo preg_replace( "/\/\*[\s\S]+?\*\//" , "" , file_get_contents ( "./Public/js/php/config.json" )); } else { echo json_encode( array ( 'state' => '请登录!' )); } exit ; } echo conditionChoice( $this ->uid); } |
放在ThinkPHP的Common.php中:
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
/** * 条件选择 * @param */ function conditionChoice( $uid ){ $CONFIG = json_decode(preg_replace( "/\/\*[\s\S]+?\*\//" , "" , file_get_contents ( "./Public/js/php/config.json" )), true); $action = $_GET [ 'action' ]; $Qiniu = C( 'QINIU' ); $domain = $Qiniu [ 'domain' ]; switch ( $action ){ case 'config' : $result = json_encode( $CONFIG ); break ; case 'uploadimage' : case 'uploadscrawl' : case 'uploadvideo' : case 'uploadfile' : $result = Upload( $CONFIG , $uid ); // 加上公网引用地址 $result = json_decode( $result ,JSON_UNESCAPED_UNICODE); if ( strtolower ( strrchr ( $result [ 'url' ], '.' )) != '.gif' ){ $result [ 'url' ] = $domain . $result [ 'url' ]. '_600.jpg' ; } else { $result [ 'url' ] = $domain . $result [ 'url' ]; } $result = json_encode( $result ,JSON_UNESCAPED_UNICODE); break ; case 'listimage' : $result = Lists( $uid ); break ; case 'listfile' : $result = json_encode( array ( 'state' => 'error 1000' )); break ; case 'catchimage' : $result = json_encode( array ( 'state' => 'error 1001' )); break ; default : $result = json_encode( array ( 'state' => '请求地址出错' )); break ; } if (isset( $_GET [ "callback" ])){ if (preg_match( "/^[\w_]+$/" , $_GET [ "callback" ])){ return htmlspecialchars( $_GET [ "callback" ]) . '(' . $result . ')' ; } else { return json_encode( array ( 'state' => 'callback参数不合法' )); } } else { return $result ; } } /** * 获取文件列表 * @param */ function Lists( $uid ){ $limit = 40; $size = isset( $_GET [ 'size' ]) ? htmlspecialchars( $_GET [ 'size' ]) : $limit ; $start = isset( $_GET [ 'start' ]) ? htmlspecialchars( $_GET [ 'start' ]) : 0; $end = $start + $size ; $list = getfiles( $start , $limit , $uid ); if (! count ( $list )){ return json_encode( array ( "state" => "no match file" , "list" => array (), "start" => $start , "total" => count ( $list ) )); } $result = json_encode( array ( "state" => "SUCCESS" , "list" => $list , "start" => $start , "total" => count ( $list ) )); return $result ; } /** * 获取指定文件列表 * @param int $start 开始位置 * @param int $limit 显示的条数 * @param int $uid 用户id */ function getfiles( $start , $limit , $uid ){ Vendor( 'Qiniu.autoload' ); $Qiniu = C( 'QINIU' ); $accessKey = $Qiniu [ 'accessKey' ]; $secretKey = $Qiniu [ 'secretKey' ]; $bucket = $Qiniu [ 'bucket' ]; $domain = $Qiniu [ 'domain' ]; $auth = new \Qiniu\Auth( $accessKey , $secretKey ); $bucketMgr = new \Qiniu\Storage\BucketManager( $auth ); // 要列取文件的公共前缀 if ( $uid ){ $prefix = 'test/' . $uid . '/' ; } else { return array (); } // 上次列举返回的位置标记,作为本次列举的起点信息。 $marker = $start ; // 本次列举的条目数 $limit = $limit ? $limit : 0; // 列举文件 list( $iterms , $marker , $err ) = $bucketMgr ->listFiles( $bucket , $prefix , $marker , $limit ); if ( $err !== null){ return array (); } else { $arr = array (); for ( $i = 0; $i < count ( $iterms ); $i ++){ if ( strtolower ( strrchr ( $iterms [ $i ][ 'key' ], '.' )) == '.gif' ){ $arr [ $i ][ 'url' ] = $domain . $iterms [ $i ][ 'key' ]; } else { $arr [ $i ][ 'url' ] = $domain . $iterms [ $i ][ 'key' ]. '_600.jpg' ; } $arr [ $i ][ 'mtime' ] = $iterms [ $i ][ 'putTime' ]; } return $arr ; } } /** * 上传文件 * @param */ function Upload( $CONFIG , $uid ){ if (! $uid ){ return json_encode( array ( "state" => 'error 1002' )); } $base64 = "upload" ; switch (htmlspecialchars( $_GET [ 'action' ])) { case 'uploadimage' : $config = array ( "pathFormat" => $CONFIG [ 'imagePathFormat' ], "maxSize" => $CONFIG [ 'imageMaxSize' ], "allowFiles" => $CONFIG [ 'imageAllowFiles' ] ); $fieldName = $CONFIG [ 'imageFieldName' ]; break ; case 'uploadscrawl' : $config = array ( "pathFormat" => $CONFIG [ 'scrawlPathFormat' ], "maxSize" => $CONFIG [ 'scrawlMaxSize' ], "allowFiles" => $CONFIG [ 'scrawlAllowFiles' ], "oriName" => "scrawl.png" ); $fieldName = $CONFIG [ 'scrawlFieldName' ]; $base64 = "base64" ; break ; case 'uploadvideo' : $config = array ( "pathFormat" => $CONFIG [ 'videoPathFormat' ], "maxSize" => $CONFIG [ 'videoMaxSize' ], "allowFiles" => $CONFIG [ 'videoAllowFiles' ] ); $fieldName = $CONFIG [ 'videoFieldName' ]; break ; case 'uploadfile' : default : $config = array ( "pathFormat" => $CONFIG [ 'filePathFormat' ], "maxSize" => $CONFIG [ 'fileMaxSize' ], "allowFiles" => $CONFIG [ 'fileAllowFiles' ] ); $fieldName = $CONFIG [ 'fileFieldName' ]; break ; } $prefix = 'test/' . $uid . '/' ; $key = $prefix .time(); return json_encode(UpCondition( $fieldName , $config , $key )); } /** * 文件判断 * @param string $fileField 表单名称 * @param config $config 配置文件 * @param config $key 新文件名称 */ function UpCondition( $fileField , $config , $key ){ $file = $_FILES [ $fileField ]; if (! $file ){ return array ( "state" => '找不到上传文件' ); } if ( $file [ 'error' ]){ return array ( "state" => $file [ 'error' ]); } else if (! file_exists ( $file [ 'tmp_name' ])) { return array ( "state" => '找不到临时文件' ); } else if (! is_uploaded_file ( $file [ 'tmp_name' ])) { return array ( "state" => '不是上传文件' ); } // 文件后缀 $ext = strtolower ( strrchr ( $file [ 'name' ], '.' )); //检查文件大小是否超出限制 if ( $file [ 'file' ] > $config [ "maxSize" ]){ return array ( "state" => '文件过大' ); } //检查是否不允许的文件格式 if (!in_array( $ext , $config [ "allowFiles" ])){ return array ( "state" => '文件格式不允许' ); } $url = $key . $ext ; $title = substr ( $url , strrpos ( $url , '/' ) + 1); //移动文件 $ret = QiniuUpload( $file [ "tmp_name" ], $url ); if ( $ret [ 'status' ] == 1){ return array ( "state" => 'SUCCESS' , 'url' => $url , 'title' => $title , 'original' => $file [ 'name' ], 'type' => $ext , 'size' => $file [ 'size' ], ); } else { return array ( "state" => '上传失败' ); } } /** * 上传文件 * @param $filePath 要移动的文件位置 * @param $key 新文件名称 */ function QiniuUpload( $filePath , $key ){ Vendor( 'Qiniu.autoload' ); $Qiniu = C( 'QINIU' ); $accessKey = $Qiniu [ 'accessKey' ]; $secretKey = $Qiniu [ 'secretKey' ]; $bucket = $Qiniu [ 'bucket' ]; // 初始化签权对象 $auth = new \Qiniu\Auth( $accessKey , $secretKey ); $token = $auth ->uploadToken( $bucket ); $uploadMgr = new \Qiniu\Storage\UploadManager(); list( $ret , $err ) = $uploadMgr ->putFile( $token , $key , $filePath ); if ( $err !== null){ $err [ 'status' ] = 0; return $err ; } else { $ret [ 'status' ] = 1; return $ret ; } } |
帅的无语!