1

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.

1
  • 1
    Hi @RishiKumar you are stopping your script with wit dd() (as it means dump and die() - which ends the execution). So you only see the first iteration of your loop. Commented Aug 16, 2022 at 19:20

1 Answer 1

3

You're killing the loop with dd

If you want to store the path in array, you should do this:

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

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.