2222 */
2323function normalizeUploadedFiles (array $ files )
2424{
25+ /**
26+ * Normalize an array of file specifications.
27+ *
28+ * Loops through all nested files (as determined by receiving an array to the
29+ * `tmp_name` key of a `$_FILES` specification) and returns a normalized array
30+ * of UploadedFile instances.
31+ *
32+ * This function normalizes a `$_FILES` array representing a nested set of
33+ * uploaded files as produced by the php-fpm SAPI, CGI SAPI, or mod_php
34+ * SAPI.
35+ *
36+ * @param array $files
37+ * @return UploadedFile[]
38+ */
39+ $ normalizeUploadedFileSpecification = function (array $ files = []) {
40+ if (! isset ($ files ['tmp_name ' ]) || ! is_array ($ files ['tmp_name ' ])
41+ || ! isset ($ files ['size ' ]) || ! is_array ($ files ['size ' ])
42+ || ! isset ($ files ['error ' ]) || ! is_array ($ files ['error ' ])
43+ ) {
44+ throw new InvalidArgumentException (sprintf (
45+ '$files provided to %s MUST contain each of the keys "tmp_name", '
46+ . ' "size", and "error", with each represented as an array; '
47+ . ' one or more were missing or non-array values ' ,
48+ __FUNCTION__
49+ ));
50+ }
51+
52+
53+ $ normalized = [];
54+ foreach (array_keys ($ files ['tmp_name ' ]) as $ key ) {
55+ $ spec = [
56+ 'tmp_name ' => $ files ['tmp_name ' ][$ key ],
57+ 'size ' => $ files ['size ' ][$ key ],
58+ 'error ' => $ files ['error ' ][$ key ],
59+ 'name ' => isset ($ files ['name ' ][$ key ]) ? $ files ['name ' ][$ key ] : null ,
60+ 'type ' => isset ($ files ['type ' ][$ key ]) ? $ files ['type ' ][$ key ] : null ,
61+ ];
62+ $ normalized [$ key ] = createUploadedFile ($ spec );
63+ }
64+ return $ normalized ;
65+ };
66+
2567 $ normalized = [];
2668 foreach ($ files as $ key => $ value ) {
2769 if ($ value instanceof UploadedFileInterface) {
@@ -30,7 +72,7 @@ function normalizeUploadedFiles(array $files)
3072 }
3173
3274 if (is_array ($ value ) && isset ($ value ['tmp_name ' ]) && is_array ($ value ['tmp_name ' ])) {
33- $ normalized [$ key ] = normalizeUploadedFileSpecification ($ value );
75+ $ normalized [$ key ] = $ normalizeUploadedFileSpecification ($ value );
3476 continue ;
3577 }
3678
0 commit comments