-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUploadedFile.php
More file actions
316 lines (283 loc) · 9.59 KB
/
UploadedFile.php
File metadata and controls
316 lines (283 loc) · 9.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
<?php
/*
* This file is part of Berlioz framework.
*
* @license https://opensource.org/licenses/MIT MIT License
* @copyright 2021 Ronan GIRON
* @author Ronan GIRON <https://github.com/ElGigi>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code, to the root.
*/
declare(strict_types=1);
namespace Berlioz\Http\Message;
use Berlioz\Http\Message\Factory\UploadedFileFactoryTrait;
use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileInterface;
use RuntimeException;
/**
* Class UploadedFile.
*/
class UploadedFile implements UploadedFileInterface
{
protected bool $moved = false;
/**
* Parse uploaded files from $_FILES PHP environment variable
*
* @param array $uploadedFiles $_FILES value
*
* @return array Multi dimensional \Berlioz\Http\Message\UploadedFile array
* @deprecated 2.6.0 Use \Berlioz\Http\Message\Factory\UploadedFileFactoryTrait::createUploadedFiles() instead.
*/
public static function parseUploadedFiles(array $uploadedFiles): array
{
$factory = new class {
use UploadedFileFactoryTrait;
};
return $factory->createUploadedFiles($uploadedFiles);
}
/**
* UploadedFile constructor
*
* @param string $file File
* @param string|null $name Client name of file
* @param string|null $type Client type of file
* @param int|null $size Size
* @param int $error PHP error code
*/
public function __construct(
protected string $file,
protected ?string $name,
protected ?string $type,
protected ?int $size,
protected int $error,
private ?StreamInterface $stream = null,
) {
}
/**
* If uploaded file has been moved
*
* @return bool
*/
public function hasMoved(): bool
{
return $this->moved;
}
/**
* Is uploaded file?
*
* @return bool
*/
public function isUploadedFile(): bool
{
return is_uploaded_file($this->file);
}
/**
* Retrieve a stream representing the uploaded file.
*
* This method MUST return a StreamInterface instance, representing the
* uploaded file. The purpose of this method is to allow utilizing native PHP
* stream functionality to manipulate the file upload, such as
* stream_copy_to_stream() (though the result will need to be decorated in a
* native PHP stream wrapper to work with such functions).
*
* If the moveTo() method has been called previously, this method MUST raise
* an exception.
*
* @return StreamInterface Stream representation of the uploaded file.
* @throws RuntimeException in cases when no stream is available or can be
* created.
*/
public function getStream(): StreamInterface
{
if ($this->hasMoved()) {
throw new RuntimeException(sprintf('Uploaded file "%s" has already moved', $this->file));
}
if (null === $this->stream) {
$this->stream = new Stream(fopen($this->file, 'r'));
}
return $this->stream;
}
/**
* Set stream of uploaded file.
*
* @param StreamInterface $stream
*
* @return UploadedFile
* @deprecated 2.6.0 Use constructor instead.
*/
public function setStream(StreamInterface $stream): UploadedFile
{
if ($this->hasMoved()) {
throw new RuntimeException(sprintf('Uploaded file "%s" has already moved', $this->file));
}
$this->stream = $stream;
return $this;
}
/**
* Move the uploaded file to a new location.
*
* Use this method as an alternative to move_uploaded_file(). This method is
* guaranteed to work in both SAPI and non-SAPI environments.
* Implementations must determine which environment they are in, and use the
* appropriate method (move_uploaded_file(), rename(), or a stream
* operation) to perform the operation.
*
* $targetPath may be an absolute path, or a relative path. If it is a
* relative path, resolution should be the same as used by PHP's rename()
* function.
*
* The original file or stream MUST be removed on completion.
*
* If this method is called more than once, any subsequent calls MUST raise
* an exception.
*
* When used in an SAPI environment where $_FILES is populated, when writing
* files via moveTo(), is_uploaded_file() and move_uploaded_file() SHOULD be
* used to ensure permissions and upload status are verified correctly.
*
* If you wish to move to a stream, use getStream(), as SAPI operations
* cannot guarantee writing to stream destinations.
*
* @see http://php.net/is_uploaded_file
* @see http://php.net/move_uploaded_file
*
* @param string $targetPath Path to which to move the uploaded file.
*
* @throws InvalidArgumentException if the $targetPath specified is invalid.
* @throws RuntimeException on any error during the move operation, or on
* the second or subsequent call to the method.
*/
public function moveTo($targetPath): void
{
if ($this->hasMoved()) {
throw new RuntimeException(sprintf('Uploaded file "%s" has already moved', $this->file));
}
$directory = dirname($targetPath);
if (!is_dir($directory)) {
if (!mkdir($directory, 0777, true)) {
throw new RuntimeException(sprintf('Error during directory creation "%s"', $directory));
}
}
if (!is_writable($directory)) {
throw new InvalidArgumentException(sprintf('Target path "%s" is not writable', $directory));
}
if (!$this->isUploadedFile()) {
throw new RuntimeException(sprintf('"%s" is not a valid uploaded file', $this->file));
}
$this->moved = move_uploaded_file($this->file, $targetPath);
}
/**
* Retrieve the file size.
*
* Implementations SHOULD return the value stored in the "size" key of
* the file in the $_FILES array if available, as PHP calculates this based
* on the actual size transmitted.
*
* @return int|null The file size in bytes or null if unknown.
*/
public function getSize(): ?int
{
return $this->size;
}
/**
* Retrieve the hash value using the contents of file.
*
* @param string $algo Name of selected hashing algorithm (i.e. "md5", "sha256", "haval160,4", etc..)
*
* @return string
* @throws RuntimeException on any error during the hash operation.
*
* @see \hash_file()
*/
public function getHash($algo = 'sha1'): string
{
if ($this->hasMoved()) {
throw new RuntimeException(sprintf('Uploaded file "%s" has already moved', $this->file));
}
return hash_file($algo, $this->file);
}
/**
* Retrieve the media type of file.
*
* @return string|null The media type or null if unavailable
* @throws RuntimeException on any error during the mime extraction operation.
*/
public function getMediaType(): ?string
{
if ($this->error !== UPLOAD_ERR_OK) {
return null;
}
if ($this->hasMoved()) {
throw new RuntimeException(sprintf('Uploaded file "%s" has already moved', $this->file));
}
if (!extension_loaded('fileinfo')) {
throw new RuntimeException(
sprintf(
'You must install fileinfo extension to determine the real type of uploaded file "%s"',
$this->file
)
);
}
$finfo = finfo_open(FILEINFO_MIME);
$mime = finfo_file($finfo, $this->file);
if (PHP_VERSION_ID < 80500) {
finfo_close($finfo);
}
$mime = explode(";", $mime);
return trim($mime[0]);
}
/**
* Retrieve the error associated with the uploaded file.
*
* The return value MUST be one of PHP's UPLOAD_ERR_XXX constants.
*
* If the file was uploaded successfully, this method MUST return
* UPLOAD_ERR_OK.
*
* Implementations SHOULD return the value stored in the "error" key of
* the file in the $_FILES array.
*
* @see http://php.net/manual/en/features.file-upload.errors.php
* @return int One of PHP's UPLOAD_ERR_XXX constants.
*/
public function getError(): int
{
return $this->error;
}
/**
* Retrieve the filename sent by the client.
*
* Do not trust the value returned by this method. A client could send
* a malicious filename with the intention to corrupt or hack your
* application.
*
* Implementations SHOULD return the value stored in the "name" key of
* the file in the $_FILES array.
*
* @return string|null The filename sent by the client or null if none
* was provided.
*/
public function getClientFilename(): ?string
{
return $this->name;
}
/**
* Retrieve the media type sent by the client.
*
* Do not trust the value returned by this method. A client could send
* a malicious media type with the intention to corrupt or hack your
* application.
*
* Implementations SHOULD return the value stored in the "type" key of
* the file in the $_FILES array.
*
* @return string|null The media type sent by the client or null if none
* was provided.
*/
public function getClientMediaType(): ?string
{
return $this->type;
}
}