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 ); } } |
thinkphp和ueditor自定义后台处理方法整合的更多相关文章
- (转) ThinkPHP模板自定义标签使用方法
这篇文章主要介绍了ThinkPHP模板自定义标签使用方法,需要的朋友可以参考下 转之--http://www.jb51.net/article/51584.htm 使用模板标签可以让网站前台开发 ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(九):历史任务查询
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(八):完成个人任务
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(七):任务列表展示
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(六):启动流程
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(五):流程定义列表
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(三):流程模型列表展示
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 (2)创建流程模型:activiti自定义流程之Spring ...
- activiti自定义流程之Spring整合activiti-modeler5.16实例(二):创建流程模型
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建 1.maven导包,这里就没有什么多的好说了,直接代码: <depe ...
随机推荐
- 【学习笔记】初识FreeMarker简单使用
楔子: 之前在和同事讨论,同事说“jsp技术太古老了,有几种页面技术代替,比如FreeMarker.Velocity.thymeleaf,jsp快废弃了……”云云.我这一听有点心虚……我在后端部分越刨 ...
- [php]mysqli操作流程
<?php class SqlTools{ private $con; private $trans; public function __construct($host, $user, $ps ...
- 学号20155308 2016-2017-2 《Java程序设计》第6周学习总结
学号20155308 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 输入与输出 目的:文件的读写:网络上传数据的基础:同样要掌握父类中方法. 10. ...
- #error#优化#Model#理解下面这句话错误所导致的错误:"传入一个对象,那么你就拥有了对象的属性"2
CHENYILONG Blog #error#优化#Model#理解下面这句话错误所导致的错误:"传入一个对象,那么你就拥有了对象的属性"2 © chenyilong. Power ...
- select标签的描述
1.标签html与js如下代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"&g ...
- 14、BigInteger类简介
BigInteger类概述 BigInteger类可以让超过Integer范围的数据进行运算,通常在对数字计算比较大的行业中应用的多一些. package com.sutaoyu.usually_cl ...
- AopProxyUtils.getSingletonTarget(Ljava/lang/Object;)Ljava/lang/Object;大坑
这个问题太坑了,试了好多个版本,都是依赖冲突导致的, https://blog.csdn.net/qq_15003505/article/details/78430595 最后找到这一篇博客解决了,就 ...
- HTTP::Request 用 add-content 添加 POST参数
sub MAIN($cmd) { use HTTP::UserAgent; my $r = HTTP::Request.new(); $r.uri: 'http://localhost/a.php'; ...
- 内核定时器的使用(好几个例子add_timer)【转】
转自:http://blog.csdn.net/jidonghui/article/details/7449546 LINUX内核定时器是内核用来控制在未来某个时间点(基于jiffies)调度执行某个 ...
- MVVM模式的 数据绑定
数据绑定要达到的效果 数据绑定要达到什么效果呢,就是在界面中绑定了数据源之后,数据在界面上的修改能反映到绑定源,同时绑定源的修改也能反映到界面上.从界面反映到绑定的数据源是很容易理解的,因为在绑定过程 ...