Uploading multiple files with Laravel

I never thought the day would come, yet here we are: I’m writing a tutorial on how to program something because somehow all the other tutorials on the internet are worse than what I can come up with.

Are you looking for a way to upload multiple files with Laravel? Then you have come to the right place! In this tutorial I am going to help you upload (and process) multiple files with Laravel using a form.

There are a lot of things that Laravel doesn’t tell you. One of those things is that $request->file(‘somename’) doesn’t just have to contain one file. It can also contain multiple files (in the form of an array), as long as you sent multiple files in the post request that called your Controller.

In this tutorial, I am going to be skipping all the junk that other people love adding to their “helpful” articles while getting straight to the point.

Preparing your form

Add enctype Form attribute

The first step is to change your form so that it’s ready to send multiple files. This is not a “Laravel thing” but rather an HTML thing, and will only work if your chosen method is “POST”.

Add the attribute “enctype” to your form, and set its value to “multipart/form-data”. You can see an example below from a form of mine that submits multiple files to a form that’s never used because the users are afraid of reality.

Of course, your action will be different from mine.

Your form tag would look like this example:

<form action="{{url('fixed/contracts/submit')}}" method="POST" enctype="multipart/form-data">

Changing your input fields

Next step is to change your input fields and to prepare for the multiple files. I am no Javascript Guru so what I did was simply copy and paste the input field for the field a few times. Use whatever method you like. I’m not your boss.

However, the key ingredient is that you need to change the name of your input field so it resembles an array. You do this by adding brackets [] to every field you’re using.

For example, if your input field used to look like this…

<input type="file" class="form-file" id="customFile" name="contractattachment">

You will need to change into this

<input type="file" class="form-file" id="customFile" name="contractattachment[]">

Changing your controller

As you already know, you can use $request->file(‘somename’) to do things with the uploaded file, such as store it to your Storage. While I am fairly sure that you can probably store all files inside the array at once, that was not a risk I was willing to take.

Remember me mentioning that $request->file(‘whatever’) isn’t just a file but can also be an array? When you uploaded multiple files, you can simply loop over the array and do stuff for every individual file.

An example would look like this:

$files = $request->file('contractattachment');
        
        foreach($files as $upload)
            {
                $path = $upload->store("Cases/Uploads/$contractid");
                DB::table('CaseFiles')->insert(['CaseID'=> $contractid, 'Filepath' => $path]);
            }

First, we assigned the values of the array to $files because foreach() loops seem to *hate* the $request->file(‘something’) request. That should make our life easier. Then, we loop over the file and do whatever it is we wanted to do with the file(s). In my example, I am storing the files to a location Cases/Uploads/$contractid and afterwards I’m writing the path of the file to a database.

You should, of course, replace these commands with anything you want.

Once the change to the controller is made, it’s time to test your new form that’ll support using multple files. You’re welcome!

Extra stuff

Wondering why I’m storing those filepaths in a database? Why, so I can easily retrieve them later of course! I’m assigning each path to an unique(?) ID that’s associated with the form upload so I can retrieve all stored files later on, using that unique ID. Simple yet effective.

Discover more from PowerUser Guide

Subscribe now to keep reading and get access to the full archive.

Continue reading