TimiAde's avatar

Multiple File Upload only returns a single File in Laravel 9

I am only seeing a single file from the form. my form is

<form action="{{ route('add_reply', $thread)}}" method="POST" enctype="multipart/form-data">
                    @csrf
                    <div class="form-group">
                        <textarea name="body" id="reply" placeholder="Lets get started"></textarea>
                    </div>
                    <div class="w-full">
                        <p class="text-red-500 font-medium text-center text-base uppercase" id="file-error"></p>
                        <label
                            class="flex flex-wrap justify-around flex-row md:flex-col items-center w-full  p-4 transition bg-white border-2 border-gray-300 border-dashed rounded-md appearance-none cursor-pointer hover:border-gray-400 focus:outline-none">
                            <div id="upload-div">
                                <div class="text-sm flex justify-center items-center flex-col w-44 h-44 border-2 border-dashed hover:border-gray-400 border-gray-300">
                                        <p class="p-0 m-0">Images</p>
                                        <p class="p-0 m-0">max(2000kb)</p>   
                                        <div class="flex justify-center items-center w-10 h-10 border-2 border-dashed border-green-400 rounded-full">
                                            <i class="fa-solid fa-plus"></i>
                                        </div>
                                </div>
                            </div>
                            <input type="file" name="file_upload" class="hidden" id="file" accept=".jpg, .jpeg, .png" multiple="true"/>
                           
                        </label>
                    </div>
                    <div class="pt-row">
                       
                        <div class="col-auto">
                            <a href="#" onclick="$(this).closest('form').submit()" class="btn btn-secondary btn-width-lg">Reply</a>
                        </div>
                    </div>
                </form>

while my store controller

 /**
     * Store a newly created resource in storage.
     *
     * @param  \App\Http\Requests\StoreCommentRequest  $request
     * @return \Illuminate\Http\Response
     */
 public function store(StoreCommentRequest $request, Thread $thread)
    {
       
        dd($request->file_upload);
}

what i am seeing in my view is

Illuminate\Http\UploadedFile {#1327 ▼
  -test: false
  -originalName: "imgonline-com-ua-resize-SfNIbKOgetJwmI3.jpg"
  -mimeType: "image/jpeg"
  -error: 0
  #hashName: null
  path: "/private/var/folders/xy/x1wf4c9n31v1j97478cd7ygm0000gn/T"
  filename: "php0IEqlw"
  basename: "php0IEqlw"
  pathname: "/private/var/folders/xy/x1wf4c9n31v1j97478cd7ygm0000gn/T/php0IEqlw"
  extension: ""
  realPath: "/private/var/folders/xy/x1wf4c9n31v1j97478cd7ygm0000gn/T/php0IEqlw"
  aTime: 2022-09-22 13:32:29
  mTime: 2022-09-22 13:32:29
  cTime: 2022-09-22 13:32:29
  inode: 30602222
  size: 431972
  perms: 0100600
  owner: 501
  group: 20
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
}

and i choosed three images even viewed them.

0 likes
11 replies
ramonrietdijk's avatar

What if you use the following instead:

dd($request->file('file_upload'));

Does that give you a list?

thinkverse's avatar

Each input needs to be of the same name and end with []. Otherwise, you'll only get one of the files added.

<input type="file" name="file_upload[]" class="hidden" id="file" accept=".jpg, .jpeg, .png" multiple="true"/>

Even with multiple the name still needs to end with [] for it to work correctly.

1 like
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

The syntax is either

<input type="file" name="file_upload" class="hidden" id="file" accept=".jpg, .jpeg, .png" multiple />
//or
<input type="file" name="file_upload" class="hidden" id="file" accept=".jpg, .jpeg, .png" multiple="multiple" />
Sinnbeck's avatar

@TimiAde both should work. The multiple keyword works alone in modern browsers :)

TimiAde's avatar

@Sinnbeck the issue was in my form i suppose to add this attribute

multipart = ""

Please or to participate in this conversation.