Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

How can I use the store method for multiple files?

I am not sure if the issue is in the PHP side or maybe the JS only sends one file.

I have a simple form for uploading files:

<form enctype="multipart/form-data">
    <input name="files" type="file" multiple>
    <input type="button" value="Upload" />
</form>

Then the ajax:

        $.ajax({
            // Your server script to process the upload
            url: '/upload',
            type: 'POST',
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            },

            // Form data
            data: new FormData($('form')[0]),
           // .. rest of the ajax ..

In the Controller, it always shows as if there is only 1 file in the $request when I dd the request.

I am not sure if the issue is from JS or because the following store method supports only single file uploads:

  $path = $request->file('files')->store('folder')

Is the issue coming from the JS part? Or it's the store method that can support only single file uploads?

0 likes
3 replies
Snapey's avatar
Snapey
Best Answer
Level 122

try change this

<input name="files[]" type="file" multiple>

ddi($request->files) in the controller to check what you got

1 like
Ligonsker's avatar

@Snapey thank you, that worked!

But now I get Call to a member function store() on array because it seem to only support single file upload.

If I want to store multiple files, I will probably need to loop through the request? (Unless Laravel has a method that supports batch upload)

Is something like the following the only way, or I can use a more convenient way like store()?

        if ($request->hasFile('files')) {           
            foreach ($request->file('files') as $file) {
                $name = <some_hash>;
                $file->move(storage_path('uploaded_media/admin') , $name);
                $data[] = $name;
            }
        }

Please or to participate in this conversation.