pordonez's avatar

Laravel get all files in a directory

I would like to download all files in a directory

public function downloadcontract()
    {
        $path = public_path() . '/contracts/';

        return response()->download($path);
    }

but when i tried this it says "Failed - Server problem"

0 likes
5 replies
aurawindsurfing's avatar

Hi @pordonez

Use Storage facade for you local filesystem and first:

Get all the files within that directory:

$files = Storage::allFiles($directory);

Then download one by one:

return Storage::download('file.jpg');

I can not see how you could download all of them at once since for browser to download a file it needs to get it as a response. Maybe you could zip the directory first?

Hope it helps!

pordonez's avatar

@AURAWINDSURFING - the problem is im not saving the file names to my database, i just want to download everything on that directory.

aurawindsurfing's avatar

This will give you list of all the files. No DB needed.

$files = Storage::allFiles($directory);
aurawindsurfing's avatar

No I do not think so. You would have to get the list of the files, add them to one zip file and download that one file.

You can not download more then one file per one browser refresh as far as I understand.

You could just create a list of a href links to each wile and download them manually I suppose.

Hope it helps!

Please or to participate in this conversation.