1 | <?php |
||||||
2 | |||||||
3 | declare(strict_types=1); |
||||||
4 | |||||||
5 | namespace HttpSoft\Message; |
||||||
6 | |||||||
7 | use InvalidArgumentException; |
||||||
8 | use Psr\Http\Message\StreamInterface; |
||||||
9 | use Psr\Http\Message\UploadedFileInterface; |
||||||
10 | use RuntimeException; |
||||||
11 | |||||||
12 | use function array_key_exists; |
||||||
13 | use function array_keys; |
||||||
14 | use function dirname; |
||||||
15 | use function fclose; |
||||||
16 | use function fopen; |
||||||
17 | use function fwrite; |
||||||
18 | use function gettype; |
||||||
19 | use function get_class; |
||||||
20 | use function implode; |
||||||
21 | use function is_dir; |
||||||
22 | use function is_object; |
||||||
23 | use function is_resource; |
||||||
24 | use function is_string; |
||||||
25 | use function is_writable; |
||||||
26 | use function move_uploaded_file; |
||||||
27 | use function rename; |
||||||
28 | use function sprintf; |
||||||
29 | use function strpos; |
||||||
30 | |||||||
31 | use const PHP_SAPI; |
||||||
32 | use const UPLOAD_ERR_OK; |
||||||
33 | use const UPLOAD_ERR_INI_SIZE; |
||||||
34 | use const UPLOAD_ERR_FORM_SIZE; |
||||||
35 | use const UPLOAD_ERR_PARTIAL; |
||||||
36 | use const UPLOAD_ERR_NO_FILE; |
||||||
37 | use const UPLOAD_ERR_NO_TMP_DIR; |
||||||
38 | use const UPLOAD_ERR_CANT_WRITE; |
||||||
39 | use const UPLOAD_ERR_EXTENSION; |
||||||
40 | |||||||
41 | final class UploadedFile implements UploadedFileInterface |
||||||
42 | { |
||||||
43 | /** |
||||||
44 | * @const array |
||||||
45 | * @link https://www.php.net/manual/en/features.file-upload.errors.php |
||||||
46 | */ |
||||||
47 | private const ERRORS = [ |
||||||
48 | UPLOAD_ERR_OK => 'There is no error, the file uploaded with success.', |
||||||
49 | UPLOAD_ERR_INI_SIZE => 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', |
||||||
50 | UPLOAD_ERR_FORM_SIZE => 'The uploaded file exceeds the MAX_FILE_SIZE directive' |
||||||
51 | . ' that was specified in the HTML form.', |
||||||
52 | UPLOAD_ERR_PARTIAL => 'The uploaded file was only partially uploaded.', |
||||||
53 | UPLOAD_ERR_NO_FILE => 'No file was uploaded.', |
||||||
54 | UPLOAD_ERR_NO_TMP_DIR => 'Missing a temporary folder.', |
||||||
55 | UPLOAD_ERR_CANT_WRITE => 'Failed to write file to disk.', |
||||||
56 | UPLOAD_ERR_EXTENSION => 'A PHP extension stopped the file upload.', |
||||||
57 | ]; |
||||||
58 | |||||||
59 | /** |
||||||
60 | * @var StreamInterface|null |
||||||
61 | */ |
||||||
62 | private ?StreamInterface $stream = null; |
||||||
63 | |||||||
64 | /** |
||||||
65 | * @var string|null |
||||||
66 | */ |
||||||
67 | private ?string $file = null; |
||||||
68 | |||||||
69 | /** |
||||||
70 | * @var int |
||||||
71 | */ |
||||||
72 | private int $size; |
||||||
73 | |||||||
74 | /** |
||||||
75 | * @var int |
||||||
76 | */ |
||||||
77 | private int $error; |
||||||
78 | |||||||
79 | /** |
||||||
80 | * @var string|null |
||||||
81 | */ |
||||||
82 | private ?string $clientFilename; |
||||||
83 | |||||||
84 | /** |
||||||
85 | * @var string|null |
||||||
86 | */ |
||||||
87 | private ?string $clientMediaType; |
||||||
88 | |||||||
89 | /** |
||||||
90 | * @var bool |
||||||
91 | */ |
||||||
92 | private bool $isMoved = false; |
||||||
93 | |||||||
94 | /** |
||||||
95 | * @param StreamInterface|string|resource $streamOrFile |
||||||
96 | * @param int $size |
||||||
97 | * @param int $error |
||||||
98 | * @param string|null $clientFilename |
||||||
99 | * @param string|null $clientMediaType |
||||||
100 | * @psalm-suppress RedundantConditionGivenDocblockType |
||||||
101 | */ |
||||||
102 | 47 | public function __construct( |
|||||
103 | $streamOrFile, |
||||||
104 | int $size, |
||||||
105 | int $error, |
||||||
106 | string $clientFilename = null, |
||||||
107 | string $clientMediaType = null |
||||||
108 | ) { |
||||||
109 | 47 | if (!array_key_exists($error, self::ERRORS)) { |
|||||
110 | 4 | throw new InvalidArgumentException(sprintf( |
|||||
111 | '"%s" is not valid error status for "UploadedFile". It must be one of "UPLOAD_ERR_*" constants: "%s".', |
||||||
112 | 4 | $error, |
|||||
113 | 4 | implode('", "', array_keys(self::ERRORS)) |
|||||
114 | )); |
||||||
115 | } |
||||||
116 | |||||||
117 | 43 | $this->size = $size; |
|||||
118 | 43 | $this->error = $error; |
|||||
119 | 43 | $this->clientFilename = $clientFilename; |
|||||
120 | 43 | $this->clientMediaType = $clientMediaType; |
|||||
121 | |||||||
122 | 43 | if ($error !== UPLOAD_ERR_OK) { |
|||||
123 | 14 | return; |
|||||
124 | } |
||||||
125 | |||||||
126 | 29 | if (is_string($streamOrFile)) { |
|||||
127 | 2 | $this->file = $streamOrFile; |
|||||
128 | 2 | return; |
|||||
129 | } |
||||||
130 | |||||||
131 | 28 | if (is_resource($streamOrFile)) { |
|||||
132 | $this->stream = new Stream($streamOrFile); |
||||||
133 | return; |
||||||
134 | } |
||||||
135 | |||||||
136 | 28 | if ($streamOrFile instanceof StreamInterface) { |
|||||
137 | 28 | $this->stream = $streamOrFile; |
|||||
138 | 28 | return; |
|||||
139 | } |
||||||
140 | |||||||
141 | throw new InvalidArgumentException(sprintf( |
||||||
142 | '"%s" is not valid stream or file provided for "UploadedFile".', |
||||||
143 | (is_object($streamOrFile) ? get_class($streamOrFile) : gettype($streamOrFile)) |
||||||
144 | )); |
||||||
145 | } |
||||||
146 | |||||||
147 | /** |
||||||
148 | * {@inheritdoc} |
||||||
149 | * |
||||||
150 | * @psalm-suppress PossiblyNullArgument |
||||||
151 | */ |
||||||
152 | 13 | public function getStream(): StreamInterface |
|||||
153 | { |
||||||
154 | 13 | if ($this->error !== UPLOAD_ERR_OK) { |
|||||
155 | 7 | throw new RuntimeException(self::ERRORS[$this->error]); |
|||||
156 | } |
||||||
157 | |||||||
158 | 6 | if ($this->isMoved) { |
|||||
159 | 2 | throw new RuntimeException('The stream is not available because it has been moved.'); |
|||||
160 | } |
||||||
161 | |||||||
162 | 4 | if ($this->stream === null) { |
|||||
163 | 1 | $this->stream = new Stream($this->file); |
|||||
164 | } |
||||||
165 | |||||||
166 | 4 | return $this->stream; |
|||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||||||
167 | } |
||||||
168 | |||||||
169 | /** |
||||||
170 | * {@inheritdoc} |
||||||
171 | * |
||||||
172 | * @psalm-suppress DocblockTypeContradiction |
||||||
173 | */ |
||||||
174 | 23 | public function moveTo($targetPath): void |
|||||
175 | { |
||||||
176 | 23 | if ($this->error !== UPLOAD_ERR_OK) { |
|||||
177 | 7 | throw new RuntimeException(self::ERRORS[$this->error]); |
|||||
178 | } |
||||||
179 | |||||||
180 | 16 | if ($this->isMoved) { |
|||||
181 | 2 | throw new RuntimeException('The file cannot be moved because it has already been moved.'); |
|||||
182 | } |
||||||
183 | |||||||
184 | 16 | if (!is_string($targetPath)) { |
|||||
185 | 9 | throw new InvalidArgumentException(sprintf( |
|||||
186 | '"%s" is not valid target path for move. It must be a string type.', |
||||||
187 | 9 | (is_object($targetPath) ? get_class($targetPath) : gettype($targetPath)) |
|||||
188 | )); |
||||||
189 | } |
||||||
190 | |||||||
191 | 7 | if (empty($targetPath)) { |
|||||
192 | throw new InvalidArgumentException('Target path is not valid for move. It must be a non-empty string.'); |
||||||
193 | } |
||||||
194 | |||||||
195 | 7 | $targetDirectory = dirname($targetPath); |
|||||
196 | |||||||
197 | 7 | if (!is_dir($targetDirectory) || !is_writable($targetDirectory)) { |
|||||
198 | throw new RuntimeException(sprintf( |
||||||
199 | 'The target directory "%s" does not exist or is not writable.', |
||||||
200 | $targetDirectory |
||||||
201 | )); |
||||||
202 | } |
||||||
203 | |||||||
204 | 7 | $this->moveOrWriteFile($targetPath); |
|||||
205 | 7 | $this->isMoved = true; |
|||||
206 | 7 | } |
|||||
207 | |||||||
208 | /** |
||||||
209 | * {@inheritdoc} |
||||||
210 | */ |
||||||
211 | 4 | public function getSize(): ?int |
|||||
212 | { |
||||||
213 | 4 | return $this->size; |
|||||
214 | } |
||||||
215 | |||||||
216 | /** |
||||||
217 | * {@inheritdoc} |
||||||
218 | */ |
||||||
219 | 3 | public function getError(): int |
|||||
220 | { |
||||||
221 | 3 | return $this->error; |
|||||
222 | } |
||||||
223 | |||||||
224 | /** |
||||||
225 | * {@inheritdoc} |
||||||
226 | */ |
||||||
227 | 4 | public function getClientFilename(): ?string |
|||||
228 | { |
||||||
229 | 4 | return $this->clientFilename; |
|||||
230 | } |
||||||
231 | |||||||
232 | /** |
||||||
233 | * {@inheritdoc} |
||||||
234 | */ |
||||||
235 | 4 | public function getClientMediaType(): ?string |
|||||
236 | { |
||||||
237 | 4 | return $this->clientMediaType; |
|||||
238 | } |
||||||
239 | |||||||
240 | /** |
||||||
241 | * Moves if used in an SAPI environment where $_FILES is populated, when writing |
||||||
242 | * files via is_uploaded_file() and move_uploaded_file() or writes If SAPI is not used. |
||||||
243 | * |
||||||
244 | * @param string $targetPath |
||||||
245 | * @psalm-suppress PossiblyNullReference |
||||||
246 | */ |
||||||
247 | 7 | private function moveOrWriteFile(string $targetPath): void |
|||||
248 | { |
||||||
249 | 7 | if ($this->file) { |
|||||
250 | $isCliEnv = (!PHP_SAPI || strpos(PHP_SAPI, 'cli') === 0 || strpos(PHP_SAPI, 'phpdbg') === 0); |
||||||
251 | |||||||
252 | if (!($isCliEnv ? rename($this->file, $targetPath) : move_uploaded_file($this->file, $targetPath))) { |
||||||
253 | throw new RuntimeException(sprintf('Uploaded file could not be moved to "%s".', $targetPath)); |
||||||
254 | } |
||||||
255 | |||||||
256 | return; |
||||||
257 | } |
||||||
258 | |||||||
259 | 7 | if (!$file = fopen($targetPath, 'wb+')) { |
|||||
260 | throw new RuntimeException(sprintf('Unable to write to "%s".', $targetPath)); |
||||||
261 | } |
||||||
262 | |||||||
263 | 7 | $this->stream->rewind(); |
|||||
0 ignored issues
–
show
The method
rewind() does not exist on null .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed.
Loading history...
|
|||||||
264 | |||||||
265 | 7 | while (!$this->stream->eof()) { |
|||||
266 | 7 | fwrite($file, $this->stream->read(512000)); |
|||||
267 | } |
||||||
268 | |||||||
269 | 7 | fclose($file); |
|||||
270 | 7 | } |
|||||
271 | } |
||||||
272 |