Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
8 / 8 |
CRAP | |
100.00% |
1 / 1 |
UploadedFile | |
100.00% |
14 / 14 |
|
100.00% |
8 / 8 |
8 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
5 / 5 |
|
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 | * @package Dynart\Micro |
12 | */ |
13 | class UploadedFile { |
14 | |
15 | protected $name; |
16 | protected $tempPath; |
17 | protected $error; |
18 | protected $size; |
19 | protected $type; |
20 | |
21 | public function __construct(string $name, string $tempPath, int $error, string $type, int $size) { |
22 | $this->name = $name; |
23 | $this->tempPath = $tempPath; |
24 | $this->error = $error; |
25 | $this->type = $type; |
26 | $this->size = $size; |
27 | } |
28 | |
29 | /** |
30 | * The original name of the uploaded file |
31 | * @return string |
32 | */ |
33 | public function name(): string { |
34 | return $this->name; |
35 | } |
36 | |
37 | /** |
38 | * The temp path of the uploaded file |
39 | * @return string |
40 | */ |
41 | public function tempPath(): string { |
42 | return $this->tempPath; |
43 | } |
44 | |
45 | /** |
46 | * The upload error. If no error happened the value is UPLOAD_ERR_OK (0) |
47 | * @return int |
48 | */ |
49 | public function error(): int { |
50 | return $this->error; |
51 | } |
52 | |
53 | /** |
54 | * The size of the uploaded file in bytes |
55 | * @return int |
56 | */ |
57 | public function size(): int { |
58 | return $this->size; |
59 | } |
60 | |
61 | /** |
62 | * The mime type of the uploaded file |
63 | * |
64 | * Important: do NOT trust this value, this is just set by the browser. If you need the real |
65 | * mime type, you should analyze the file for it! |
66 | * |
67 | * @return string |
68 | */ |
69 | public function type(): string { |
70 | return $this->type; |
71 | } |
72 | |
73 | /** |
74 | * Tells whether the file was uploaded via HTTP POST |
75 | * @return bool |
76 | */ |
77 | public function uploaded(): bool { |
78 | return is_uploaded_file($this->tempPath); |
79 | } |
80 | |
81 | /** |
82 | * Moves the uploaded file to the given path, the sets the tempPath to '' |
83 | * @param string $path The destination |
84 | * @return bool Is the move was successful? |
85 | */ |
86 | public function moveTo(string $path): bool { |
87 | $result = move_uploaded_file($this->tempPath, $path); |
88 | $this->tempPath = ''; |
89 | return $result; |
90 | } |
91 | |
92 | } |