1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace System\Http; |
4
|
|
|
|
5
|
|
|
use System\Application; |
6
|
|
|
use Exception; |
7
|
|
|
|
8
|
|
|
class UploadeFile |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Application Object |
12
|
|
|
* |
13
|
|
|
* @var \System\Application |
14
|
|
|
*/ |
15
|
|
|
private $app; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* File |
19
|
|
|
* |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $file = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* File Name |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $fileName; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* File Name |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $nameOnly; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* File Extension |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
private $extension; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* File Minetype |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $minetype; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* File Temp |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $tempFile; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* File Size |
61
|
|
|
* |
62
|
|
|
* @var string |
63
|
|
|
*/ |
64
|
|
|
private $size; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* File Error |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
private $error; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Constructor |
75
|
|
|
* |
76
|
|
|
* @param \System\Application $app |
77
|
|
|
* @param $file |
78
|
|
|
*/ |
79
|
|
|
public function __construct(Application $app) |
80
|
|
|
{ |
81
|
|
|
$this->app = $app; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Recive the file key |
86
|
|
|
* |
87
|
|
|
* @param string $file |
88
|
|
|
* @return object $this; |
89
|
|
|
*/ |
90
|
|
|
public function file(string $file) |
91
|
|
|
{ |
92
|
|
|
$this->setFileInfo($file); |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Set the variables |
99
|
|
|
* |
100
|
|
|
* @property object $request |
101
|
|
|
* @param string $file |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
|
|
private function setFileInfo($file) |
105
|
|
|
{ |
106
|
|
|
$this->file = $this->app->request->file($file); |
|
|
|
|
107
|
|
|
|
108
|
|
|
$this->error = $this->file['error']; |
109
|
|
|
|
110
|
|
|
if ($this->error != UPLOAD_ERR_OK) { |
111
|
|
|
throw new Exception('Something went wrong'); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$this->fileName = $this->file['name']; |
115
|
|
|
|
116
|
|
|
$this->minetype = $this->file['type']; |
117
|
|
|
|
118
|
|
|
$this->size = $this->file['size']; |
119
|
|
|
|
120
|
|
|
$this->tempFile = $this->file['tmp_name']; |
121
|
|
|
|
122
|
|
|
$fileInfo = pathinfo($this->fileName); |
123
|
|
|
|
124
|
|
|
$this->nameOnly = $fileInfo['filename']; |
125
|
|
|
|
126
|
|
|
$this->extension = $fileInfo['extension']; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get file name |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
|
|
public function getFileName() |
135
|
|
|
{ |
136
|
|
|
return $this->fileName; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get file name only |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
|
|
*/ |
144
|
|
|
public function getNameOnly() |
145
|
|
|
{ |
146
|
|
|
return $this->nameOnly; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Get extension |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function getExtension() |
155
|
|
|
{ |
156
|
|
|
return $this->extension; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get minetype |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public function getMinetype() |
165
|
|
|
{ |
166
|
|
|
return $this->minetype; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Get size |
171
|
|
|
* |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
public function getSize() |
175
|
|
|
{ |
176
|
|
|
return $this->size; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Get temp file |
181
|
|
|
* |
182
|
|
|
* @return string |
183
|
|
|
*/ |
184
|
|
|
public function getTempFile() |
185
|
|
|
{ |
186
|
|
|
return $this->tempFile; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Move file to the geven target |
191
|
|
|
* |
192
|
|
|
* @param string $target |
193
|
|
|
* @param string $newName |
194
|
|
|
* @return bool |
195
|
|
|
*/ |
196
|
|
|
public function moveTo(string $target, string $newName = null) |
197
|
|
|
{ |
198
|
|
|
$newName = $newName ?: sha1(rand()) . sha1(rand()); |
199
|
|
|
$newName .= '.' . $this->extension; |
200
|
|
|
|
201
|
|
|
if (!is_dir($target)) { |
202
|
|
|
mkdir($target, 0777, true); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$filePath = $target . $newName; |
206
|
|
|
$filePath = rtrim($filePath, '/'); |
207
|
|
|
$filePath = ltrim($filePath, '/'); |
208
|
|
|
|
209
|
|
|
return (bool) move_uploaded_file($this->tempFile, $filePath); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.