1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved. |
6
|
|
|
// +---------------------------------------------------------------------- |
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
8
|
|
|
// +---------------------------------------------------------------------- |
9
|
|
|
// | Author: liu21st <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
|
12
|
|
|
namespace think; |
13
|
|
|
|
14
|
|
|
use SplFileObject; |
15
|
|
|
|
16
|
|
|
class File extends SplFileObject |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* 错误信息 |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $error = ''; |
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* 当前完整文件名 |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $filename; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* 上传文件名 |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $saveName; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* 上传文件命名规则 |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected $rule = 'date'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* 上传文件验证规则 |
44
|
|
|
* @var array |
45
|
|
|
*/ |
46
|
|
|
protected $validate = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* 是否单元测试 |
50
|
|
|
* @var bool |
51
|
|
|
*/ |
52
|
|
|
protected $isTest; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* 上传文件信息 |
56
|
|
|
* @var array |
57
|
|
|
*/ |
58
|
|
|
protected $info = []; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* 文件hash规则 |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
protected $hash = []; |
65
|
|
|
|
66
|
|
|
public function __construct($filename, $mode = 'r') |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
parent::__construct($filename, $mode); |
69
|
|
|
|
70
|
|
|
$this->filename = $this->getRealPath() ?: $this->getPathname(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* 是否测试 |
75
|
|
|
* @access public |
76
|
|
|
* @param bool $test 是否测试 |
77
|
|
|
* @return $this |
78
|
|
|
*/ |
79
|
|
|
public function isTest($test = false) |
80
|
|
|
{ |
81
|
|
|
$this->isTest = $test; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* 设置上传信息 |
88
|
|
|
* @access public |
89
|
|
|
* @param array $info 上传文件信息 |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
|
|
public function setUploadInfo($info) |
93
|
|
|
{ |
94
|
|
|
$this->info = $info; |
95
|
|
|
|
96
|
|
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* 获取上传文件的信息 |
101
|
|
|
* @access public |
102
|
|
|
* @param string $name |
|
|
|
|
103
|
|
|
* @return array|string |
104
|
|
|
*/ |
105
|
|
|
public function getInfo($name = '') |
106
|
|
|
{ |
107
|
|
|
return isset($this->info[$name]) ? $this->info[$name] : $this->info; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* 获取上传文件的文件名 |
112
|
|
|
* @access public |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function getSaveName() |
116
|
|
|
{ |
117
|
|
|
return $this->saveName; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* 设置上传文件的保存文件名 |
122
|
|
|
* @access public |
123
|
|
|
* @param string $saveName |
|
|
|
|
124
|
|
|
* @return $this |
125
|
|
|
*/ |
126
|
|
|
public function setSaveName($saveName) |
127
|
|
|
{ |
128
|
|
|
$this->saveName = $saveName; |
129
|
|
|
|
130
|
|
|
return $this; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* 获取文件的哈希散列值 |
135
|
|
|
* @access public |
136
|
|
|
* @param string $type |
|
|
|
|
137
|
|
|
* @return string |
138
|
|
|
*/ |
139
|
|
|
public function hash($type = 'sha1') |
140
|
|
|
{ |
141
|
|
|
if (!isset($this->hash[$type])) { |
142
|
|
|
$this->hash[$type] = hash_file($type, $this->filename); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $this->hash[$type]; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* 检查目录是否可写 |
150
|
|
|
* @access protected |
151
|
|
|
* @param string $path 目录 |
152
|
|
|
* @return boolean |
153
|
|
|
* @throws \Exception |
154
|
|
|
*/ |
155
|
|
|
protected function checkPath($path) { |
|
|
|
|
156
|
|
|
if (is_dir($path)) { |
157
|
|
|
return true; |
158
|
|
|
} |
159
|
|
|
try { |
160
|
|
|
if (!mkdir($path, 0755, true)) { |
161
|
|
|
$this->error = ['directory {:path} creation failed', ['path' => $path]]; |
162
|
|
|
return false; |
163
|
|
|
}; |
164
|
|
|
} catch (\Exception $e) { |
165
|
|
|
if ($e->getMessage() === 'mkdir(): File exists') { |
166
|
|
|
return true; |
167
|
|
|
}; |
168
|
|
|
throw $e; |
169
|
|
|
} |
170
|
|
|
return true; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* 获取文件类型信息 |
175
|
|
|
* @access public |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
|
|
public function getMime() |
179
|
|
|
{ |
180
|
|
|
$finfo = finfo_open(FILEINFO_MIME_TYPE); |
181
|
|
|
|
182
|
|
|
return finfo_file($finfo, $this->filename); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* 设置文件的命名规则 |
187
|
|
|
* @access public |
188
|
|
|
* @param string $rule 文件命名规则 |
|
|
|
|
189
|
|
|
* @return $this |
190
|
|
|
*/ |
191
|
|
|
public function rule($rule) |
192
|
|
|
{ |
193
|
|
|
$this->rule = $rule; |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* 设置上传文件的验证规则 |
200
|
|
|
* @access public |
201
|
|
|
* @param array $rule 验证规则 |
|
|
|
|
202
|
|
|
* @return $this |
203
|
|
|
*/ |
204
|
|
|
public function validate($rule = []) |
205
|
|
|
{ |
206
|
|
|
$this->validate = $rule; |
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* 检测是否合法的上传文件 |
213
|
|
|
* @access public |
214
|
|
|
* @return bool |
215
|
|
|
*/ |
216
|
|
|
public function isValid() |
217
|
|
|
{ |
218
|
|
|
if ($this->isTest) { |
219
|
|
|
return is_file($this->filename); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return is_uploaded_file($this->filename); |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* 检测上传文件 |
227
|
|
|
* @access public |
228
|
|
|
* @param array $rule 验证规则 |
|
|
|
|
229
|
|
|
* @return bool |
230
|
|
|
*/ |
231
|
|
|
public function check($rule = []) |
232
|
|
|
{ |
233
|
|
|
$rule = $rule ?: $this->validate; |
234
|
|
|
|
235
|
|
|
if ((isset($rule['size']) && !$this->checkSize($rule['size'])) |
236
|
|
|
|| (isset($rule['type']) && !$this->checkMime($rule['type'])) |
237
|
|
|
|| (isset($rule['ext']) && !$this->checkExt($rule['ext'])) |
238
|
|
|
|| !$this->checkImg()) { |
|
|
|
|
239
|
|
|
return false; |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return true; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* 检测上传文件后缀 |
247
|
|
|
* @access public |
248
|
|
|
* @param array|string $ext 允许后缀 |
|
|
|
|
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
|
|
public function checkExt($ext) |
252
|
|
|
{ |
253
|
|
|
if (is_string($ext)) { |
254
|
|
|
$ext = explode(',', $ext); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
$extension = strtolower(pathinfo($this->getInfo('name'), PATHINFO_EXTENSION)); |
258
|
|
|
|
259
|
|
|
if (!in_array($extension, $ext)) { |
260
|
|
|
$this->error = 'extensions to upload is not allowed'; |
261
|
|
|
return false; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return true; |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* 检测图像文件 |
269
|
|
|
* @access public |
270
|
|
|
* @return bool |
271
|
|
|
*/ |
272
|
|
|
public function checkImg() |
273
|
|
|
{ |
274
|
|
|
$extension = strtolower(pathinfo($this->getInfo('name'), PATHINFO_EXTENSION)); |
275
|
|
|
|
276
|
|
|
/* 对图像文件进行严格检测 */ |
277
|
|
|
if (in_array($extension, ['gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf']) && !in_array($this->getImageType($this->filename), [1, 2, 3, 4, 6, 13])) { |
278
|
|
|
$this->error = 'illegal image files'; |
279
|
|
|
return false; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
return true; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
// 判断图像类型 |
286
|
|
|
protected function getImageType($image) |
|
|
|
|
287
|
|
|
{ |
288
|
|
|
if (function_exists('exif_imagetype')) { |
289
|
|
|
return exif_imagetype($image); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
try { |
293
|
|
|
$info = getimagesize($image); |
294
|
|
|
return $info ? $info[2] : false; |
295
|
|
|
} catch (\Exception $e) { |
296
|
|
|
return false; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* 检测上传文件大小 |
302
|
|
|
* @access public |
303
|
|
|
* @param integer $size 最大大小 |
|
|
|
|
304
|
|
|
* @return bool |
305
|
|
|
*/ |
306
|
|
|
public function checkSize($size) |
307
|
|
|
{ |
308
|
|
|
if ($this->getSize() > (int) $size) { |
309
|
|
|
$this->error = 'filesize not match'; |
310
|
|
|
return false; |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
return true; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* 检测上传文件类型 |
318
|
|
|
* @access public |
319
|
|
|
* @param array|string $mime 允许类型 |
|
|
|
|
320
|
|
|
* @return bool |
321
|
|
|
*/ |
322
|
|
|
public function checkMime($mime) |
323
|
|
|
{ |
324
|
|
|
if (is_string($mime)) { |
325
|
|
|
$mime = explode(',', $mime); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
if (!in_array(strtolower($this->getMime()), $mime)) { |
329
|
|
|
$this->error = 'mimetype to upload is not allowed'; |
330
|
|
|
return false; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
return true; |
334
|
|
|
} |
335
|
|
|
|
336
|
|
|
/** |
337
|
|
|
* 移动文件 |
338
|
|
|
* @access public |
339
|
|
|
* @param string $path 保存路径 |
|
|
|
|
340
|
|
|
* @param string|bool $savename 保存的文件名 默认自动生成 |
|
|
|
|
341
|
|
|
* @param boolean $replace 同名文件是否覆盖 |
|
|
|
|
342
|
|
|
* @param bool $autoAppendExt 自动补充扩展名 |
|
|
|
|
343
|
|
|
* @return false|File false-失败 否则返回File实例 |
344
|
|
|
*/ |
345
|
|
|
public function move($path, $savename = true, $replace = true, $autoAppendExt = true, $) |
|
|
|
|
346
|
|
|
{ |
347
|
|
|
// 文件上传失败,捕获错误代码 |
348
|
|
|
if (!empty($this->info['error'])) { |
349
|
|
|
$this->error($this->info['error']); |
350
|
|
|
return false; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
// 检测合法性 |
354
|
|
|
if (!$this->isValid()) { |
355
|
|
|
$this->error = 'upload illegal files'; |
356
|
|
|
return false; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
// 验证上传 |
360
|
|
|
if (!$this->check()) { |
361
|
|
|
return false; |
362
|
|
|
} |
363
|
|
|
|
364
|
|
|
$path = rtrim($path, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR; |
365
|
|
|
// 文件保存命名规则 |
366
|
|
|
$saveName = $this->buildSaveName($savename, $autoAppendExt); |
367
|
|
|
$filename = $path . $saveName; |
368
|
|
|
|
369
|
|
|
// 检测目录 |
370
|
|
|
if (false === $this->checkPath(dirname($filename))) { |
371
|
|
|
return false; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
/* 不覆盖同名文件 */ |
375
|
|
|
if (!$replace && is_file($filename)) { |
376
|
|
|
$this->error = ['has the same filename: {:filename}', ['filename' => $filename]]; |
377
|
|
|
return false; |
378
|
|
|
} |
379
|
|
|
|
380
|
|
|
/* 移动文件 */ |
381
|
|
|
if ($this->isTest) { |
382
|
|
|
rename($this->filename, $filename); |
383
|
|
|
} elseif (!move_uploaded_file($this->filename, $filename)) { |
384
|
|
|
$this->error = 'upload write error'; |
385
|
|
|
return false; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
// 返回 File对象实例 |
389
|
|
|
$file = new self($filename); |
390
|
|
|
$file->setSaveName($saveName); |
391
|
|
|
$file->setUploadInfo($this->info); |
392
|
|
|
|
393
|
|
|
return $file; |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* 获取保存文件名 |
398
|
|
|
* @access protected |
399
|
|
|
* @param string|bool $savename 保存的文件名 默认自动生成 |
|
|
|
|
400
|
|
|
* @param bool $autoAppendExt 自动补充扩展名 |
|
|
|
|
401
|
|
|
* @return string |
402
|
|
|
*/ |
403
|
|
|
protected function buildSaveName($savename, $autoAppendExt = true) |
404
|
|
|
{ |
405
|
|
|
if (true === $savename) { |
406
|
|
|
// 自动生成文件名 |
407
|
|
|
$savename = $this->autoBuildName(); |
408
|
|
|
} elseif ('' === $savename || false === $savename) { |
409
|
|
|
// 保留原文件名 |
410
|
|
|
$savename = $this->getInfo('name'); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
if ($autoAppendExt && false === strpos($savename, '.')) { |
414
|
|
|
$savename .= '.' . pathinfo($this->getInfo('name'), PATHINFO_EXTENSION); |
415
|
|
|
} |
416
|
|
|
|
417
|
|
|
return $savename; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
/** |
421
|
|
|
* 自动生成文件名 |
422
|
|
|
* @access protected |
423
|
|
|
* @return string |
424
|
|
|
*/ |
425
|
|
|
protected function autoBuildName() |
426
|
|
|
{ |
427
|
|
|
if ($this->rule instanceof \Closure) { |
428
|
|
|
$savename = call_user_func_array($this->rule, [$this]); |
429
|
|
|
} else { |
430
|
|
|
switch ($this->rule) { |
431
|
|
|
case 'date': |
432
|
|
|
$savename = date('Ymd') . DIRECTORY_SEPARATOR . md5(microtime(true)); |
433
|
|
|
break; |
434
|
|
|
default: |
435
|
|
|
if (in_array($this->rule, hash_algos())) { |
436
|
|
|
$hash = $this->hash($this->rule); |
437
|
|
|
$savename = substr($hash, 0, 2) . DIRECTORY_SEPARATOR . substr($hash, 2); |
438
|
|
|
} elseif (is_callable($this->rule)) { |
439
|
|
|
$savename = call_user_func($this->rule); |
440
|
|
|
} else { |
441
|
|
|
$savename = date('Ymd') . DIRECTORY_SEPARATOR . md5(microtime(true)); |
442
|
|
|
} |
443
|
|
|
} |
444
|
|
|
} |
445
|
|
|
|
446
|
|
|
return $savename; |
447
|
|
|
} |
448
|
|
|
|
449
|
|
|
/** |
450
|
|
|
* 获取错误代码信息 |
451
|
|
|
* @access private |
452
|
|
|
* @param int $errorNo 错误号 |
|
|
|
|
453
|
|
|
*/ |
|
|
|
|
454
|
|
|
private function error($errorNo) |
|
|
|
|
455
|
|
|
{ |
456
|
|
|
switch ($errorNo) { |
457
|
|
|
case 1: |
458
|
|
|
case 2: |
459
|
|
|
$this->error = 'upload File size exceeds the maximum value'; |
460
|
|
|
break; |
461
|
|
|
case 3: |
462
|
|
|
$this->error = 'only the portion of file is uploaded'; |
463
|
|
|
break; |
464
|
|
|
case 4: |
465
|
|
|
$this->error = 'no file to uploaded'; |
466
|
|
|
break; |
467
|
|
|
case 6: |
468
|
|
|
$this->error = 'upload temp dir not found'; |
469
|
|
|
break; |
470
|
|
|
case 7: |
471
|
|
|
$this->error = 'file write error'; |
472
|
|
|
break; |
473
|
|
|
default: |
474
|
|
|
$this->error = 'unknown upload error'; |
475
|
|
|
} |
476
|
|
|
} |
477
|
|
|
|
478
|
|
|
/** |
479
|
|
|
* 获取错误信息(支持多语言) |
480
|
|
|
* @access public |
481
|
|
|
* @return string |
482
|
|
|
*/ |
483
|
|
|
public function getError() |
484
|
|
|
{ |
485
|
|
|
$lang = Container::get('lang'); |
486
|
|
|
|
487
|
|
|
if (is_array($this->error)) { |
488
|
|
|
list($msg, $vars) = $this->error; |
489
|
|
|
} else { |
490
|
|
|
$msg = $this->error; |
491
|
|
|
$vars = []; |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
return $lang->has($msg) ? $lang->get($msg, $vars) : $msg; |
495
|
|
|
} |
496
|
|
|
|
497
|
|
|
public function __call($method, $args) |
|
|
|
|
498
|
|
|
{ |
499
|
|
|
return $this->hash($method); |
500
|
|
|
} |
501
|
|
|
} |
502
|
|
|
|