thinkphp和ueditor自定义后台处理方法整合
先了解一下ueditor后台请求参数与返回参数格式规范:
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
|
1. config 请求参数: GET { "action" : "config" } POST "upfile" : File Data 返回格式: // 需要支持callback参数,返回jsonp格式 { "imageUrl" : "http://localhost/ueditor/php/controller.php?action=uploadimage" , "imagePath" : "/ueditor/php/" , "imageFieldName" : "upfile" , "imageMaxSize" : 2048, "imageAllowFiles" : [ ".png" , ".jpg" , ".jpeg" , ".gif" , ".bmp" ] } 2. uploadimage 请求参数: GET { "action" : "uploadimage" } POST "upfile" : File Data 返回格式: { "state" : "SUCCESS" , "url" : "upload/demo.jpg" , "title" : "demo.jpg" , "original" : "demo.jpg" } 3. uploadscrawl 请求参数: GET { "action" : "uploadscrawl" } POST "content" : Base64 Data 返回格式: { "state" : "SUCCESS" , "url" : "upload/demo.jpg" , "title" : "demo.jpg" , "original" : "demo.jpg" } 4. uploadvideo 请求参数: GET { "action" : "uploadvideo" } POST "upfile" : File Data 返回格式: { "state" : "SUCCESS" , "url" : "upload/demo.mp4" , "title" : "demo.mp4" , "original" : "demo.mp4" } 5. uploadfile 请求参数: GET { "action" : "uploadfile" } POST "upfile" : File Data 返回格式: { "state" : "SUCCESS" , "url" : "upload/demo.zip" , "title" : "demo.zip" , "original" : "demo.zip" } 6. listimage 请求参数: GET { "action" : "listimage" , "start" : 0, "size" : 20} 返回格式: // 需要支持callback参数,返回jsonp格式 { "state" : "SUCCESS" , "list" : [{ "url" : "upload/1.jpg" }, { "url" : "upload/2.jpg" }, ], "start" : 20, "total" : 100 } 7. catchimage 请求参数: GET { "action" : "catchimage" , "source" : [ "http://a.com/1.jpg" , "http://a.com/2.jpg" ] } 返回格式: // 需要支持callback参数,返回jsonp格式 // list项的state属性和最外面的state格式一致 { "state" : "SUCCESS" , "list" : [{ "url" : "upload/1.jpg" , "source" : "http://b.com/2.jpg" , "state" : "SUCCESS" }, { "url" : "upload/2.jpg" , "source" : "http://b.com/2.jpg" , "state" : "SUCCESS" }, ] } |
ueditor后台统一处理方法
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
<?php namespace Home\Controller; use Think\Controller; class IndexController extends Controller { private $uid = 0; //用户id /** * ueditor上传后台处理方法 * @param */ public function UploadSomething(){ header( "Content-Type: text/html; charset=utf-8" ); error_reporting (E_ERROR); // 未登录状态 if ( $this ->uid == 0){ if ( $_GET [ 'action' ] == 'config' ){ // 读取并输出配置文件,用于未登录状态editor加载初始化 echo preg_replace( "/\/\*[\s\S]+?\*\//" , "" , file_get_contents ( "./Public/js/php/config.json" )); } else { // 上传请求时,提示登录 echo json_encode( array ( 'state' => '请登录!' )); } exit ; } // 登录状态 $CONFIG = json_decode(preg_replace( "/\/\*[\s\S]+?\*\//" , "" , file_get_contents ( "./Public/js/php/config.json" )), true); $action = $_GET [ 'action' ]; switch ( $action ){ case 'config' : $result = json_encode( $CONFIG ); break ; case 'uploadimage' : case 'uploadscrawl' : case 'uploadvideo' : case 'uploadfile' : /** * 上传图片或文件处理方法,返回数据格式为json,具体格式如下 * return json_encode( array( "state" => "", //上传状态,上传成功时必须返回"SUCCESS" "url" => "", //返回的地址 "title" => "", //新文件名 "original" => "", //原始文件名 "type" => "", //文件类型 "size" => "", //文件大小 ) ); */ $result = $this ->ueditorUpload( $CONFIG ); break ; /** * 列表操作:返回的数据格式,列出图片或文件 array( "state" => "SUCCESS", // 成功返回信息 "start" => $start, // 开始位置 "total" => count($files), // 文件个数统计 // 当前列出的文件列表 "list" => array( array('url' => '图片地址','mtime' => '时间戳'), array('url' => '图片地址','mtime' => '时间戳'), ); ) */ // 列出图片 case 'listimage' : $result = $this ->ueditorList( $CONFIG ); break ; // 列出文件 case 'listfile' : $result = $this ->ueditorList( $CONFIG ); break ; /** * 抓取远程文件操作:返回的数据格式 array( 'state' => count($list) ? 'SUCCESS':'ERROR', 'list' => array( array( "state" => $info["state"], "url" => $info["url"], "size" => $info["size"], "title" => htmlspecialchars($info["title"]), "original" => htmlspecialchars($info["original"]), "source" => htmlspecialchars($imgUrl) ), array( "state" => $info["state"], "url" => $info["url"], "size" => $info["size"], "title" => htmlspecialchars($info["title"]), "original" => htmlspecialchars($info["original"]), "source" => htmlspecialchars($imgUrl) ) ) ) */ // 抓取远程文件 case 'catchimage' : $result = $this ->ueditorCrawler( $CONFIG ); break ; default : $result = json_encode( array ( 'state' => '请求地址出错' )); break ; } if (isset( $_GET [ "callback" ])){ if (preg_match( "/^[\w_]+$/" , $_GET [ "callback" ])){ echo htmlspecialchars( $_GET [ "callback" ]) . '(' . $result . ')' ; } else { echo json_encode( array ( 'state' => 'callback参数不合法' )); } } else { echo $result ; } } /** * 上传文件 * @param json $CONFIG 配置文件 * @return json { "state":"SUCCESS", "url":"返回的地址", "title":"新文件名", "original":"原始文件名", "type":"文件类型", "size":"文件大小" } */ private function ueditorUpload( $CONFIG ){ // 导入上传类 Vendor( 'Uploader' , '' , '.class.php' ); $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 ; } $up = new \Uploader( $fieldName , $config , $base64 , $this ->uid); return json_encode( $up ->getFileInfo()); } /** * 图片列表 * @param json $CONFIG 配置文件 * @return json { "state":"SUCCESS", "start":"开始位置", "total":"文件个数统计", "list":[ {"url":"图片地址","mtime":"时间戳"}, {"url":"图片地址","mtime":"时间戳"} ] } */ private function ueditorList( $CONFIG ){ switch ( $_GET [ 'action' ]) { /* 列出文件 */ case 'listfile' : $allowFiles = $CONFIG [ 'fileManagerAllowFiles' ]; $listSize = $CONFIG [ 'fileManagerListSize' ]; $path = $CONFIG [ 'fileManagerListPath' ]; break ; /* 列出图片 */ case 'listimage' : default : $allowFiles = $CONFIG [ 'imageManagerAllowFiles' ]; $listSize = $CONFIG [ 'imageManagerListSize' ]; $path = $CONFIG [ 'imageManagerListPath' ]; } $allowFiles = substr ( str_replace ( "." , "|" , join( "" , $allowFiles )), 1); /* 获取参数 */ $size = isset( $_GET [ 'size' ]) ? htmlspecialchars( $_GET [ 'size' ]) : $listSize ; $start = isset( $_GET [ 'start' ]) ? htmlspecialchars( $_GET [ 'start' ]) : 0; $end = $start + $size ; /* 获取文件列表 */ $path = $_SERVER [ 'DOCUMENT_ROOT' ] . ( substr ( $path , 0, 1) == "/" ? "" : "/" ) . $path ; $files = getfiles( $path , $allowFiles ); if (! count ( $files )){ return json_encode( array ( "state" => "no match file" , "list" => array (), "start" => $start , "total" => count ( $files ) )); } /* 获取指定范围的列表 */ $len = count ( $files ); for ( $i = min( $end , $len ) - 1, $list = array (); $i < $len && $i >= 0 && $i >= $start ; $i --){ $list [] = $files [ $i ]; } /* 返回数据 */ $result = json_encode( array ( "state" => "SUCCESS" , "list" => $list , "start" => $start , "total" => count ( $files ) )); return $result ; } /** * 抓取图片 * @param json $CONFIG 配置文件 * @return json json_encode(array( 'state' => count($list) ? 'SUCCESS':'ERROR', 'list' => array( array( "state" => $info["state"], "url" => $info["url"], "size" => $info["size"], "title" => htmlspecialchars($info["title"]), "original" => htmlspecialchars($info["original"]), "source" => htmlspecialchars($imgUrl) ), array( "state" => $info["state"], "url" => $info["url"], "size" => $info["size"], "title" => htmlspecialchars($info["title"]), "original" => htmlspecialchars($info["original"]), "source" => htmlspecialchars($imgUrl) ) ) )); */ private function ueditorCrawler( $CONFIG ){ set_time_limit(0); // 导入上传类 Vendor( 'Uploader' , '' , '.class.php' ); $config = array ( "pathFormat" => $CONFIG [ 'catcherPathFormat' ], "maxSize" => $CONFIG [ 'catcherMaxSize' ], "allowFiles" => $CONFIG [ 'catcherAllowFiles' ], "oriName" => "remote.png" ); $fieldName = $CONFIG [ 'catcherFieldName' ]; $list = array (); if (isset( $_POST [ $fieldName ])) { $source = $_POST [ $fieldName ]; } else { $source = $_GET [ $fieldName ]; } foreach ( $source as $imgUrl ) { $item = new \Uploader( $imgUrl , $config , "remote" , $this ->uid); $info = $item ->getFileInfo(); array_push ( $list , array ( "state" => $info [ "state" ], "url" => $info [ "url" ], "size" => $info [ "size" ], "title" => htmlspecialchars( $info [ "title" ]), "original" => htmlspecialchars( $info [ "original" ]), "source" => htmlspecialchars( $imgUrl ) )); } return json_encode( array ( 'state' => count ( $list ) ? 'SUCCESS' : 'ERROR' , 'list' => $list )); } } |
要调用的上传类,放在Think/Library/Vendor目录下,用Vendor()调用即可。
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
<?php /** * Created by JetBrains PhpStorm. * User: taoqili * Date: 12-7-18 * Time: 上午11: 32 * UEditor编辑器通用上传类 * 比较原百度的上传类,此方法在构造方法中加入了一个参数,用于区分文件保存路径,具体看代码 */ class Uploader { private $fileField ; //文件域名 private $file ; //文件上传对象 private $base64 ; //文件上传对象 private $config ; //配置信息 private $oriName ; //原始文件名 private $fileName ; //新文件名 private $fullName ; //完整文件名,即从当前配置目录开始的URL private $filePath ; //完整文件名,即从当前配置目录开始的URL private $fileSize ; //文件大小 private $fileType ; //文件类型 private $stateInfo ; //上传状态信息, private $uid ; // 用户id private $stateMap = array ( //上传状态映射表,国际化用户需考虑此处数据的国际化 "SUCCESS" , //上传成功标记,在UEditor中内不可改变,否则flash判断会出错 "文件大小超出 upload_max_filesize 限制" , "文件大小超出 MAX_FILE_SIZE 限制" , "文件未被完整上传" , "没有文件被上传" , "上传文件为空" , "ERROR_TMP_FILE" => "临时文件错误" , "ERROR_TMP_FILE_NOT_FOUND" => "找不到临时文件" , "ERROR_SIZE_EXCEED" => "文件大小超出网站限制" , "ERROR_TYPE_NOT_ALLOWED" => "文件类型不允许" , "ERROR_CREATE_DIR" => "目录创建失败" , "ERROR_DIR_NOT_WRITEABLE" => "目录没有写权限" , "ERROR_FILE_MOVE" => "文件保存时出错" , "ERROR_FILE_NOT_FOUND" => "找不到上传文件" , "ERROR_WRITE_CONTENT" => "写入文件内容错误" , "ERROR_UNKNOWN" => "未知错误" , "ERROR_DEAD_LINK" => "链接不可用" , "ERROR_HTTP_LINK" => "链接不是http链接" , "ERROR_HTTP_CONTENTTYPE" => "链接contentType不正确" , "INVALID_URL" => "非法 URL" , "INVALID_IP" => "非法 IP" ); /** * 构造函数 * @param string $fileField 表单名称 * @param array $config 配置项 * @param bool $base64 是否解析base64编码,可省略。若开启,则$fileField代表的是base64编码的字符串表单名 * @param int $uid 用户id,用于区分图片保存的文件夹 */ public function __construct( $fileField , $config , $type = "upload" , $uid ) { $this ->uid = $uid ; // 在293行使用到 $this ->fileField = $fileField ; $this ->config = $config ; $this ->type = $type ; if ( $type == "remote" ) { $this ->saveRemote(); } else if ( $type == "base64" ) { $this ->upBase64(); } else { $this ->upFile(); } $this ->stateMap[ 'ERROR_TYPE_NOT_ALLOWED' ] = iconv( 'unicode' , 'utf-8' , $this ->stateMap[ 'ERROR_TYPE_NOT_ALLOWED' ]); } /** * 上传文件的主处理方法 * @return mixed */ private function upFile() { $file = $this ->file = $_FILES [ $this ->fileField]; if (! $file ) { $this ->stateInfo = $this ->getStateInfo( "ERROR_FILE_NOT_FOUND" ); return ; } if ( $this ->file[ 'error' ]) { $this ->stateInfo = $this ->getStateInfo( $file [ 'error' ]); return ; } else if (! file_exists ( $file [ 'tmp_name' ])) { $this ->stateInfo = $this ->getStateInfo( "ERROR_TMP_FILE_NOT_FOUND" ); return ; } else if (! is_uploaded_file ( $file [ 'tmp_name' ])) { $this ->stateInfo = $this ->getStateInfo( "ERROR_TMPFILE" ); return ; } $this ->oriName = $file [ 'name' ]; $this -> fileSize = $file [ 'size' ]; $this -> fileType = $this ->getFileExt(); $this ->fullName = $this ->getFullName(); $this ->filePath = $this ->getFilePath(); $this ->fileName = $this ->getFileName(); $dirname = dirname( $this ->filePath); //检查文件大小是否超出限制 if (! $this ->checkSize()) { $this ->stateInfo = $this ->getStateInfo( "ERROR_SIZE_EXCEED" ); return ; } //检查是否不允许的文件格式 if (! $this ->checkType()) { $this ->stateInfo = $this ->getStateInfo( "ERROR_TYPE_NOT_ALLOWED" ); return ; } //创建目录失败 if (! file_exists ( $dirname ) && ! mkdir ( $dirname , 0777, true)) { $this ->stateInfo = $this ->getStateInfo( "ERROR_CREATE_DIR" ); return ; } else if (! is_writeable ( $dirname )) { $this ->stateInfo = $this ->getStateInfo( "ERROR_DIR_NOT_WRITEABLE" ); return ; } //移动文件 if (!(move_uploaded_file( $file [ "tmp_name" ], $this ->filePath) && file_exists ( $this ->filePath))) { //移动失败 $this ->stateInfo = $this ->getStateInfo( "ERROR_FILE_MOVE" ); } else { //移动成功 $this ->stateInfo = $this ->stateMap[0]; } } /** * 处理base64编码的图片上传 * @return mixed */ private function upBase64() { $base64Data = $_POST [ $this ->fileField]; $img = base64_decode ( $base64Data ); $this ->oriName = $this ->config[ 'oriName' ]; $this -> fileSize = strlen ( $img ); $this -> fileType = $this ->getFileExt(); $this ->fullName = $this ->getFullName(); $this ->filePath = $this ->getFilePath(); $this ->fileName = $this ->getFileName(); $dirname = dirname( $this ->filePath); //检查文件大小是否超出限制 if (! $this ->checkSize()) { $this ->stateInfo = $this ->getStateInfo( "ERROR_SIZE_EXCEED" ); return ; } //创建目录失败 if (! file_exists ( $dirname ) && ! mkdir ( $dirname , 0777, true)) { $this ->stateInfo = $this ->getStateInfo( "ERROR_CREATE_DIR" ); return ; } else if (! is_writeable ( $dirname )) { $this ->stateInfo = $this ->getStateInfo( "ERROR_DIR_NOT_WRITEABLE" ); return ; } //移动文件 if (!( file_put_contents ( $this ->filePath, $img ) && file_exists ( $this ->filePath))) { //移动失败 $this ->stateInfo = $this ->getStateInfo( "ERROR_WRITE_CONTENT" ); } else { //移动成功 $this ->stateInfo = $this ->stateMap[0]; } } /** * 拉取远程图片 * @return mixed */ private function saveRemote() { $imgUrl = htmlspecialchars( $this ->fileField); $imgUrl = str_replace ( "&" , "&" , $imgUrl ); //http开头验证 if ( strpos ( $imgUrl , "http" ) !== 0) { $this ->stateInfo = $this ->getStateInfo( "ERROR_HTTP_LINK" ); return ; } preg_match( '/(^https*:\/\/[^:\/]+)/' , $imgUrl , $matches ); $host_with_protocol = count ( $matches ) > 1 ? $matches [1] : '' ; // 判断是否是合法 url if (!filter_var( $host_with_protocol , FILTER_VALIDATE_URL)) { $this ->stateInfo = $this ->getStateInfo( "INVALID_URL" ); return ; } preg_match( '/^https*:\/\/(.+)/' , $host_with_protocol , $matches ); $host_without_protocol = count ( $matches ) > 1 ? $matches [1] : '' ; // 此时提取出来的可能是 ip 也有可能是域名,先获取 ip $ip = gethostbyname ( $host_without_protocol ); // 判断是否是私有 ip if (!filter_var( $ip , FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) { $this ->stateInfo = $this ->getStateInfo( "INVALID_IP" ); return ; } //获取请求头并检测死链 $heads = get_headers( $imgUrl , 1); if (!( stristr ( $heads [0], "200" ) && stristr ( $heads [0], "OK" ))) { $this ->stateInfo = $this ->getStateInfo( "ERROR_DEAD_LINK" ); return ; } //格式验证(扩展名验证和Content-Type验证) $fileType = strtolower ( strrchr ( $imgUrl , '.' )); if (!in_array( $fileType , $this ->config[ 'allowFiles' ]) || !isset( $heads [ 'Content-Type' ]) || ! stristr ( $heads [ 'Content-Type' ], "image" )) { $this ->stateInfo = $this ->getStateInfo( "ERROR_HTTP_CONTENTTYPE" ); return ; } //打开输出缓冲区并获取远程图片 ob_start(); $context = stream_context_create( array ( 'http' => array ( 'follow_location' => false // don't follow redirects )) ); readfile( $imgUrl , false, $context ); $img = ob_get_contents(); ob_end_clean(); preg_match( "/[\/]([^\/]*)[\.]?[^\.\/]*$/" , $imgUrl , $m ); $this ->oriName = $m ? $m [1]: "" ; $this -> fileSize = strlen ( $img ); $this -> fileType = $this ->getFileExt(); $this ->fullName = $this ->getFullName(); $this ->filePath = $this ->getFilePath(); $this ->fileName = $this ->getFileName(); $dirname = dirname( $this ->filePath); //检查文件大小是否超出限制 if (! $this ->checkSize()) { $this ->stateInfo = $this ->getStateInfo( "ERROR_SIZE_EXCEED" ); return ; } //创建目录失败 if (! file_exists ( $dirname ) && ! mkdir ( $dirname , 0777, true)) { $this ->stateInfo = $this ->getStateInfo( "ERROR_CREATE_DIR" ); return ; } else if (! is_writeable ( $dirname )) { $this ->stateInfo = $this ->getStateInfo( "ERROR_DIR_NOT_WRITEABLE" ); return ; } //移动文件 if (!( file_put_contents ( $this ->filePath, $img ) && file_exists ( $this ->filePath))) { //移动失败 $this ->stateInfo = $this ->getStateInfo( "ERROR_WRITE_CONTENT" ); } else { //移动成功 $this ->stateInfo = $this ->stateMap[0]; } } /** * 上传错误检查 * @param $errCode * @return string */ private function getStateInfo( $errCode ) { return ! $this ->stateMap[ $errCode ] ? $this ->stateMap[ "ERROR_UNKNOWN" ] : $this ->stateMap[ $errCode ]; } /** * 获取文件扩展名 * @return string */ private function getFileExt() { return strtolower ( strrchr ( $this ->oriName, '.' )); } /** * 重命名文件 * @return string */ private function getFullName() { //替换日期事件 $t = time(); $d = explode ( '-' , date ( "Y-y-m-d-H-i-s" )); $format = $this ->config[ "pathFormat" ]; $format = str_replace ( "{uid}" , $this ->uid, $format ); //uid:用户id $format = str_replace ( "{yyyy}" , $d [0], $format ); $format = str_replace ( "{yy}" , $d [1], $format ); $format = str_replace ( "{mm}" , $d [2], $format ); $format = str_replace ( "{dd}" , $d [3], $format ); $format = str_replace ( "{hh}" , $d [4], $format ); $format = str_replace ( "{ii}" , $d [5], $format ); $format = str_replace ( "{ss}" , $d [6], $format ); $format = str_replace ( "{time}" , $t , $format ); //过滤文件名的非法自负,并替换文件名 $oriName = substr ( $this ->oriName, 0, strrpos ( $this ->oriName, '.' )); $oriName = preg_replace( "/[\|\?\"\<\>\/\*\\\\]+/" , '' , $oriName ); $format = str_replace ( "{filename}" , $oriName , $format ); //替换随机字符串 $randNum = rand(1, 10000000000) . rand(1, 10000000000); if (preg_match( "/\{rand\:([\d]*)\}/i" , $format , $matches )) { $format = preg_replace( "/\{rand\:[\d]*\}/i" , substr ( $randNum , 0, $matches [1]), $format ); } $ext = $this ->getFileExt(); return $format . $ext ; } /** * 获取文件名 * @return string */ private function getFileName () { return substr ( $this ->filePath, strrpos ( $this ->filePath, '/' ) + 1); } /** * 获取文件完整路径 * @return string */ private function getFilePath() { $fullname = $this ->fullName; $rootPath = $_SERVER [ 'DOCUMENT_ROOT' ]; if ( substr ( $fullname , 0, 1) != '/' ) { $fullname = '/' . $fullname ; } return $rootPath . $fullname ; } /** * 文件类型检测 * @return bool */ private function checkType() { return in_array( $this ->getFileExt(), $this ->config[ "allowFiles" ]); } /** * 文件大小检测 * @return bool */ private function checkSize() { return $this -> fileSize <= ( $this ->config[ "maxSize" ]); } /** * 获取当前上传成功文件的各项信息 * @return array */ public function getFileInfo() { return array ( "state" => $this ->stateInfo, "url" => $this ->fullName, "title" => $this ->fileName, "original" => $this ->oriName, "type" => $this -> fileType , "size" => $this -> fileSize ); } } |
帅的无语!