Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
10 / 10 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
| UploadedFile | |
100.00% |
10 / 10 |
|
100.00% |
8 / 8 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| tempPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| error | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| size | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| type | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| uploaded | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| moveTo | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Dynart\Micro; |
| 4 | |
| 5 | /** |
| 6 | * Represents an uploaded file |
| 7 | * |
| 8 | * You can get an instance of this via the `Request::uploadedFile()` method. |
| 9 | * |
| 10 | * @see Request |
| 11 | */ |
| 12 | class UploadedFile { |
| 13 | |
| 14 | public function __construct( |
| 15 | protected string $name, |
| 16 | protected string $tempPath, |
| 17 | protected int $error, |
| 18 | protected string $type, |
| 19 | protected int $size |
| 20 | ) {} |
| 21 | |
| 22 | /** |
| 23 | * The original name of the uploaded file |
| 24 | */ |
| 25 | public function name(): string { |
| 26 | return $this->name; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * The temp path of the uploaded file |
| 31 | */ |
| 32 | public function tempPath(): string { |
| 33 | return $this->tempPath; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * The upload error. If no error happened the value is UPLOAD_ERR_OK (0) |
| 38 | */ |
| 39 | public function error(): int { |
| 40 | return $this->error; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * The size of the uploaded file in bytes |
| 45 | */ |
| 46 | public function size(): int { |
| 47 | return $this->size; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * The mime type of the uploaded file |
| 52 | * |
| 53 | * Important: do NOT trust this value, this is just set by the browser. If you need the real |
| 54 | * mime type, you should analyze the file for it! |
| 55 | */ |
| 56 | public function type(): string { |
| 57 | return $this->type; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Tells whether the file was uploaded via HTTP POST |
| 62 | */ |
| 63 | public function uploaded(): bool { |
| 64 | return is_uploaded_file($this->tempPath); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * Moves the uploaded file to the given path, then sets the tempPath to '' |
| 69 | */ |
| 70 | public function moveTo(string $path): bool { |
| 71 | $result = move_uploaded_file($this->tempPath, $path); |
| 72 | $this->tempPath = ''; |
| 73 | return $result; |
| 74 | } |
| 75 | |
| 76 | } |