1 | <?php |
||
16 | class UploadedFile implements UploadedFileInterface |
||
17 | { |
||
18 | const ERRORS = [ |
||
19 | UPLOAD_ERR_OK, |
||
20 | UPLOAD_ERR_INI_SIZE, |
||
21 | UPLOAD_ERR_FORM_SIZE, |
||
22 | UPLOAD_ERR_PARTIAL, |
||
23 | UPLOAD_ERR_NO_FILE, |
||
24 | UPLOAD_ERR_NO_TMP_DIR, |
||
25 | UPLOAD_ERR_CANT_WRITE, |
||
26 | UPLOAD_ERR_EXTENSION, |
||
27 | ]; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $clientFilename; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $clientMediaType; |
||
38 | |||
39 | /** |
||
40 | * @var int |
||
41 | */ |
||
42 | private $error; |
||
43 | |||
44 | /** |
||
45 | * @var null|string |
||
46 | */ |
||
47 | private $file; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | private $moved = false; |
||
53 | |||
54 | /** |
||
55 | * @var int |
||
56 | */ |
||
57 | private $size; |
||
58 | |||
59 | /** |
||
60 | * @var StreamInterface|null |
||
61 | */ |
||
62 | private $stream; |
||
63 | |||
64 | /** |
||
65 | * @param StreamInterface|string|resource $streamOrFile |
||
66 | * @param int $size |
||
67 | * @param int $errorStatus |
||
68 | * @param string|null $clientFilename |
||
69 | * @param string|null $clientMediaType |
||
70 | */ |
||
71 | 74 | public function __construct( |
|
87 | |||
88 | /** |
||
89 | * Depending on the value set file or stream variable |
||
90 | * |
||
91 | * @param mixed $streamOrFile |
||
92 | * @throws InvalidArgumentException |
||
93 | */ |
||
94 | 27 | private function setStreamOrFile($streamOrFile) |
|
108 | |||
109 | /** |
||
110 | * @param int $error |
||
111 | * @throws InvalidArgumentException |
||
112 | */ |
||
113 | 74 | private function setError($error) |
|
129 | |||
130 | /** |
||
131 | * @param int $size |
||
132 | * @throws InvalidArgumentException |
||
133 | */ |
||
134 | 65 | private function setSize($size) |
|
144 | |||
145 | /** |
||
146 | * @param mixed $param |
||
147 | * @return boolean |
||
148 | */ |
||
149 | 61 | private function isStringOrNull($param) |
|
153 | |||
154 | /** |
||
155 | * @param mixed $param |
||
156 | * @return boolean |
||
157 | */ |
||
158 | 12 | private function isStringNotEmpty($param) |
|
162 | |||
163 | /** |
||
164 | * @param string|null $clientFilename |
||
165 | * @throws InvalidArgumentException |
||
166 | */ |
||
167 | 61 | private function setClientFilename($clientFilename) |
|
177 | |||
178 | /** |
||
179 | * @param string|null $clientMediaType |
||
180 | * @throws InvalidArgumentException |
||
181 | */ |
||
182 | 55 | private function setClientMediaType($clientMediaType) |
|
192 | |||
193 | /** |
||
194 | * Return true if there is no upload error |
||
195 | * |
||
196 | * @return boolean |
||
197 | */ |
||
198 | 49 | private function isOk() |
|
202 | |||
203 | /** |
||
204 | * @return boolean |
||
205 | */ |
||
206 | 15 | public function isMoved() |
|
210 | |||
211 | /** |
||
212 | * @throws RuntimeException if is moved or not ok |
||
213 | */ |
||
214 | 29 | private function validateActive() |
|
224 | |||
225 | /** |
||
226 | * {@inheritdoc} |
||
227 | * @throws RuntimeException if the upload was not successful. |
||
228 | */ |
||
229 | 13 | public function getStream() |
|
239 | |||
240 | /** |
||
241 | * {@inheritdoc} |
||
242 | * |
||
243 | * @see http://php.net/is_uploaded_file |
||
244 | * @see http://php.net/move_uploaded_file |
||
245 | * @param string $targetPath Path to which to move the uploaded file. |
||
246 | * @throws RuntimeException if the upload was not successful. |
||
247 | * @throws InvalidArgumentException if the $path specified is invalid. |
||
248 | * @throws RuntimeException on any error during the move operation, or on |
||
249 | * the second or subsequent call to the method. |
||
250 | */ |
||
251 | 19 | public function moveTo($targetPath) |
|
280 | |||
281 | /** |
||
282 | * {@inheritdoc} |
||
283 | * |
||
284 | * @return int|null The file size in bytes or null if unknown. |
||
285 | */ |
||
286 | 1 | public function getSize() |
|
290 | |||
291 | /** |
||
292 | * {@inheritdoc} |
||
293 | * |
||
294 | * @see http://php.net/manual/en/features.file-upload.errors.php |
||
295 | * @return int One of PHP's UPLOAD_ERR_XXX constants. |
||
296 | */ |
||
297 | 7 | public function getError() |
|
301 | |||
302 | /** |
||
303 | * {@inheritdoc} |
||
304 | * |
||
305 | * @return string|null The filename sent by the client or null if none |
||
306 | * was provided. |
||
307 | */ |
||
308 | 1 | public function getClientFilename() |
|
312 | |||
313 | /** |
||
314 | * {@inheritdoc} |
||
315 | */ |
||
316 | 1 | public function getClientMediaType() |
|
320 | } |
||
321 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: