skip to Main Content

I’m trying to upload multiple images in one request with Laravel. However, it only seems to be seeing one file regardless of the fact that my file input has multiple files attached.

Markup:

<input class="file-input" type="file" name="fileUpload" multiple>

Controller:

$this->validate($request, [
'fileUpload' => 'image|nullable',

 ...

foreach ($request->file('fileUpload') as $image) {
    dd('stop');
}
dd('fail');

In this case, it always returns "fail". However, if I try to just $request->file('fileUpload') it will return to me a single file, like so: a single file

2

Answers


  1. Like multiple selects, if you want to send multiple values then you need to use the array syntax in your input’s name attribute. So it should be fileUpload[] instead of just fileUpload

    Login or Signup to reply.
  2. Change the name of the input fileUpload to fileUpload[] :

    <input class="file-input" type="file" name="fileUpload[]" multiple />
    

    Also in the controller for validating multiple fields, change ‘fileUpload’ to ‘fileUpload.*’ :

    $this->validate($request, [
                               'fileUpload.*' => 'image|nullable',
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search