Skip to content

Commit 0f40d22

Browse files
authored
Merge pull request Athlon1600#61 from achasseux/fixes-multiple-postedfiles
Fixes multiple posted files
2 parents 12584d6 + 3d5d814 commit 0f40d22

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

src/Http/Request.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -259,18 +259,31 @@ public static function buildPostBody($fields, $files, $boundary = null){
259259
}
260260

261261
// data better have [name, tmp_name, and optional type]
262-
foreach($files as $name => $values){
263-
264-
// There must be no error http://php.net/manual/en/features.file-upload.errors.php
265-
if(!$values['tmp_name'] || $values['error'] !== 0 || !is_readable($values['tmp_name'])){
266-
continue;
262+
foreach($files as $name => $values) {
263+
// Multiple files can be uploaded using different name for input.
264+
// See http://php.net/manual/en/features.file-upload.multiple.php
265+
if (!is_array($values['tmp_name'])) {
266+
$multiValues = array_map(function ($a) {
267+
return (array)$a;
268+
}, $values);
269+
$fieldName = $name;
270+
} else {
271+
$multiValues = $values;
272+
$fieldName = "{$name}[]";
273+
}
274+
275+
foreach (array_keys($multiValues['tmp_name']) as $key) {
276+
277+
// There must be no error http://php.net/manual/en/features.file-upload.errors.php
278+
if (!$multiValues['tmp_name'][$key] || $multiValues['error'][$key] !== 0 || !is_readable($multiValues['tmp_name'][$key])) {
279+
continue;
280+
}
281+
282+
$body .= sprintf($part_file, $boundary, $fieldName, $multiValues['name'][$key], $multiValues['type'][$key]);
283+
$body .= file_get_contents($multiValues['tmp_name'][$key]);
284+
$body .= "\r\n";
267285
}
268-
269-
$body .= sprintf($part_file, $boundary, $name, $values['name'], $values['type']);
270-
$body .= file_get_contents($values['tmp_name']);
271-
$body .= "\r\n";
272286
}
273-
274287
$body .= "--{$boundary}--\r\n\r\n";
275288

276289
return $body;

0 commit comments

Comments
 (0)