|
1
|
|
|
<?php |
|
2
|
|
|
// +---------------------------------------------------------------------- |
|
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
|
4
|
|
|
// +---------------------------------------------------------------------- |
|
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
|
6
|
|
|
// +---------------------------------------------------------------------- |
|
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
|
8
|
|
|
// +---------------------------------------------------------------------- |
|
9
|
|
|
// | Author: yunwuxin <[email protected]> |
|
10
|
|
|
// +---------------------------------------------------------------------- |
|
11
|
|
|
namespace think\file; |
|
12
|
|
|
|
|
13
|
|
|
use think\exception\FileException; |
|
14
|
|
|
use think\File; |
|
15
|
|
|
|
|
16
|
|
|
class UploadedFile extends File |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
|
|
19
|
|
|
private $test = false; |
|
|
|
|
|
|
20
|
|
|
private $originalName; |
|
|
|
|
|
|
21
|
|
|
private $mimeType; |
|
|
|
|
|
|
22
|
|
|
private $error; |
|
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
public function __construct(string $path, string $originalName, string $mimeType = null, int $error = null, $test = false) |
|
|
|
|
|
|
25
|
|
|
{ |
|
26
|
|
|
$this->originalName = $originalName; |
|
27
|
|
|
$this->mimeType = $mimeType ?: 'application/octet-stream'; |
|
28
|
|
|
$this->test = $test; |
|
29
|
|
|
$this->error = $error ?: UPLOAD_ERR_OK; |
|
30
|
|
|
parent::__construct($path, UPLOAD_ERR_OK === $this->error); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function isValid() |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$isOk = UPLOAD_ERR_OK === $this->error; |
|
36
|
|
|
|
|
37
|
|
|
return $this->test ? $isOk : $isOk && is_uploaded_file($this->getPathname()); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function move(string $directory, $name = null) |
|
|
|
|
|
|
41
|
|
|
{ |
|
42
|
|
|
if ($this->isValid()) { |
|
43
|
|
|
if ($this->test) { |
|
44
|
|
|
return parent::move($directory, $name); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$target = $this->getTargetFile($directory, $name); |
|
48
|
|
|
|
|
49
|
|
|
set_error_handler(function ($type, $msg) use (&$error) { |
|
|
|
|
|
|
50
|
|
|
$error = $msg; |
|
51
|
|
|
}); |
|
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
$moved = move_uploaded_file($this->getPathname(), $target); |
|
54
|
|
|
restore_error_handler(); |
|
55
|
|
|
if (!$moved) { |
|
56
|
|
|
throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error))); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
@chmod($target, 0666 & ~umask()); |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
return $target; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
throw new FileException($this->getErrorMessage()); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* 获取错误信息 |
|
69
|
|
|
* @access public |
|
70
|
|
|
* @return string |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function getErrorMessage(): string |
|
73
|
|
|
{ |
|
74
|
|
|
switch ($this->error) { |
|
75
|
|
|
case 1: |
|
|
|
|
|
|
76
|
|
|
case 2: |
|
|
|
|
|
|
77
|
|
|
$message = 'upload File size exceeds the maximum value'; |
|
78
|
|
|
break; |
|
79
|
|
|
case 3: |
|
|
|
|
|
|
80
|
|
|
$message = 'only the portion of file is uploaded'; |
|
81
|
|
|
break; |
|
82
|
|
|
case 4: |
|
|
|
|
|
|
83
|
|
|
$message = 'no file to uploaded'; |
|
84
|
|
|
break; |
|
85
|
|
|
case 6: |
|
|
|
|
|
|
86
|
|
|
$message = 'upload temp dir not found'; |
|
87
|
|
|
break; |
|
88
|
|
|
case 7: |
|
|
|
|
|
|
89
|
|
|
$message = 'file write error'; |
|
90
|
|
|
break; |
|
91
|
|
|
default: |
|
|
|
|
|
|
92
|
|
|
$message = 'unknown upload error'; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return $message; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* 获取上传文件类型信息 |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getOriginalMime() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->mimeType; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* 上传文件名 |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getOriginalName() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->originalName; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* 获取上传文件扩展名 |
|
118
|
|
|
* @return string |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getOriginalExtension() |
|
121
|
|
|
{ |
|
122
|
|
|
return pathinfo($this->originalName, PATHINFO_EXTENSION); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* 获取文件扩展名 |
|
127
|
|
|
* @return string |
|
128
|
|
|
*/ |
|
129
|
|
|
public function extension() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->getOriginalExtension(); |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|