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_OK; |
28
|
|
|
use const UPLOAD_ERR_NO_FILE; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Gets the request's uploaded files |
32
|
|
|
* |
33
|
|
|
* Please note that unsent files will not be handled, |
34
|
|
|
* also note that if a file fails to upload successfully, |
35
|
|
|
* a stream will not be created for it. |
36
|
|
|
* |
37
|
|
|
* @param array|null $files |
38
|
|
|
* |
39
|
|
|
* @return array |
40
|
|
|
* |
41
|
|
|
* @link http://php.net/manual/en/reserved.variables.files.php |
42
|
|
|
* @link https://www.php.net/manual/ru/features.file-upload.post-method.php |
43
|
|
|
* @link https://www.php.net/manual/ru/features.file-upload.multiple.php |
44
|
|
|
* @link https://github.com/php/php-src/blob/8c5b41cefb88b753c630b731956ede8d9da30c5d/main/rfc1867.c |
45
|
|
|
*/ |
46
|
|
|
function server_request_files(?array $files = null): array |
47
|
|
|
{ |
48
|
39 |
|
$files ??= $_FILES; |
49
|
|
|
|
50
|
39 |
|
$walker = static function ($path, $size, $error, $name, $type) use (&$walker) { |
51
|
2 |
|
if (!is_array($path)) { |
52
|
|
|
// It makes no sense to create a stream |
53
|
|
|
// if the file has not been successfully uploaded. |
54
|
2 |
|
$stream = UPLOAD_ERR_OK <> $error ? null : new FileStream($path, 'rb'); |
55
|
|
|
|
56
|
2 |
|
return new UploadedFile($stream, $size, $error, $name, $type); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
$result = []; |
60
|
1 |
|
foreach ($path as $key => $_) { |
61
|
1 |
|
if (UPLOAD_ERR_NO_FILE <> $error[$key]) { |
62
|
1 |
|
$result[$key] = $walker( |
63
|
1 |
|
$path[$key], |
64
|
1 |
|
$size[$key], |
65
|
1 |
|
$error[$key], |
66
|
1 |
|
$name[$key], |
67
|
1 |
|
$type[$key] |
68
|
1 |
|
); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
return $result; |
73
|
39 |
|
}; |
74
|
|
|
|
75
|
39 |
|
$result = []; |
76
|
39 |
|
foreach ($files as $key => $file) { |
77
|
2 |
|
if (UPLOAD_ERR_NO_FILE <> $file['error']) { |
78
|
2 |
|
$result[$key] = $walker( |
79
|
2 |
|
$file['tmp_name'], |
80
|
2 |
|
$file['size'], |
81
|
2 |
|
$file['error'], |
82
|
2 |
|
$file['name'], |
83
|
2 |
|
$file['type'] |
84
|
2 |
|
); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
39 |
|
return $result; |
89
|
|
|
} |
90
|
|
|
|