|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* It's free open-source software released under the MIT License. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Anatoly Nekhay <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2018, Anatoly Nekhay |
|
8
|
|
|
* @license https://github.com/sunrise-php/http-message/blob/master/LICENSE |
|
9
|
|
|
* @link https://github.com/sunrise-php/http-message |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sunrise\Http\Message; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Import classes |
|
16
|
|
|
*/ |
|
17
|
|
|
use Sunrise\Http\Message\Stream\FileStream; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Import functions |
|
21
|
|
|
*/ |
|
22
|
|
|
use function is_array; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Import constants |
|
26
|
|
|
*/ |
|
27
|
|
|
use const UPLOAD_ERR_NO_FILE; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Gets the request uploaded files |
|
31
|
|
|
* |
|
32
|
|
|
* Note that not sent files will not be handled. |
|
33
|
|
|
* |
|
34
|
|
|
* @param array|null $rawUploadedFiles |
|
35
|
|
|
* |
|
36
|
|
|
* @return array |
|
37
|
|
|
* |
|
38
|
|
|
* @link http://php.net/manual/en/reserved.variables.files.php |
|
39
|
|
|
* @link https://www.php.net/manual/ru/features.file-upload.post-method.php |
|
40
|
|
|
* @link https://www.php.net/manual/ru/features.file-upload.multiple.php |
|
41
|
|
|
* @link https://github.com/php/php-src/blob/8c5b41cefb88b753c630b731956ede8d9da30c5d/main/rfc1867.c |
|
42
|
|
|
*/ |
|
43
|
|
|
function server_request_files(?array $rawUploadedFiles = null): array |
|
44
|
|
|
{ |
|
45
|
39 |
|
$rawUploadedFiles ??= $_FILES; |
|
46
|
|
|
|
|
47
|
39 |
|
$walker = function ($path, $size, $error, $name, $type) use (&$walker) { |
|
48
|
2 |
|
if (! is_array($path)) { |
|
49
|
2 |
|
return new UploadedFile(new FileStream($path, 'rb'), $size, $error, $name, $type); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
$result = []; |
|
53
|
1 |
|
foreach ($path as $key => $_) { |
|
54
|
1 |
|
if (UPLOAD_ERR_NO_FILE <> $error[$key]) { |
|
55
|
1 |
|
$result[$key] = $walker($path[$key], $size[$key], $error[$key], $name[$key], $type[$key]); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
return $result; |
|
60
|
39 |
|
}; |
|
61
|
|
|
|
|
62
|
39 |
|
$result = []; |
|
63
|
39 |
|
foreach ($rawUploadedFiles as $key => $file) { |
|
64
|
2 |
|
if (UPLOAD_ERR_NO_FILE <> $file['error']) { |
|
65
|
2 |
|
$result[$key] = $walker($file['tmp_name'], $file['size'], $file['error'], $file['name'], $file['type']); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
39 |
|
return $result; |
|
70
|
|
|
} |
|
71
|
|
|
|