Home > Enterprise >  How to upload multiple files from single form input in Laravel?
How to upload multiple files from single form input in Laravel?

Time:08-18

I'm trying to upload multiple pdf files using Laravel. I've made the form using simple HTML and enabled the multiple attribute in the input field. I've tried many other methods mentioned in the stack community but they didn't work for me. In the controller, I've the following to check whether the files are being uploaded or not. The foreach loop will return the path of the files uploaded.

Controller Code

if ($request->hasFile('corpfile')) {
            $file = $request->file('corpfile');
            // dd($file);
            foreach ($file as $attachment) {
                $path = $attachment->store('corporate');
                dd($path);
            }

HTML Form Code

<input type="file" name='corpfile[]'  accept=".xls, .xlsx, .xlsm, .pdf"  multiple>

The dd($file) is returning an array with all the files uploaded but the dd($path) is returning only one file's path. I don't know what the issue is. Any help is appreciated.

CodePudding user response:

you're diying the loop with dd

if you want to store the path in array , you should do that

$path = [];
if ($request->hasFile('corpfile')) {
            $file = $request->file('corpfile');
            // dd($file);
            foreach ($file as $attachment) {
                $path[] = $attachment->store('corporate');
            }

}
  • Related