Laravel 5, trying multi-file upload, Request :: file () only returns the last file?

I am trying to get multiple files uploaded using the same key using a Laravel 5 Request

facade. From what I've read elsewhere, the correct way to do this is to call Request::file()

without passing a parameter to the method ::file()

.

However, it looks like this only returns the last file posted in the request.

Headings

POST /test/service/upload HTTP/1.1
Host: www.****.dev
X-CSRF-TOKEN: 2DQBuTuy50EELFen5vXFaOv1cyXICmAISUx8LoCS
Cache-Control: no-cache

----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="photo"; filename="10464005_10152969193248906_6272325120604924631_n.jpg"
Content-Type: image/jpeg


----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="photo"; filename="10458555_10152969192978906_1569926627111581344_n.jpg"
Content-Type: image/jpeg


----WebKitFormBoundaryE19zNvXGzXaLvS5C
Content-Disposition: form-data; name="photo"; filename="10365774_10152969188498906_1884545544754633531_n.jpg"
Content-Type: image/jpeg


----WebKitFormBoundaryE19zNvXGzXaLvS5C

      

PHP

    $files = Request::file();
    $names = [];

    foreach ($files as $file) {
        $names[] = $file->getClientOriginalName();
    }
    return $names;

      

answer

[
    "10365774_10152969188498906_1884545544754633531_n.jpg"
]

      

Is there any configuration or headers I have to set for this to work appropriately? If it helps, it will be an AJAX based request and I am using the Postman Google Chrome extension to test it.

Any help would be greatly appreciated!

+3


source to share


1 answer


use file element array as html like follow

<input type="file" name="photo[]">
<input type="file" name="photo[]">

      

add enctype attribute in form and in laravel to get the file use the file key following



$files = Request::file('photo');
    $names = [];

    foreach ($files as $file) {
        $names[] = $file->getClientOriginalName();
    }
    return $names;

      

for me it should work.

+8


source







All Articles