If you need your users to be able to download multiple files at once, it’s better to create one archive and let them download it.
In fact, it’s less about Laravel and more about PHP, we will be using ZipArchive class that existed since PHP 5.2.
Archive user’s invoice from storage/invoices/aaa001.pdfHere’s the code:$zip_file = 'invoices.zip'; // Name of our archive to download // Initializing PHP class $zip = new \ZipArchive(); $zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE); $invoice_file = 'invoices/aaa001.pdf'; // Adding file: second parameter is what will the path inside of the archive // So it will create another folder called "storage/" inside ZIP, and put the file there.
$zip->addFile(storage_path($invoice_file), $invoice_file); $zip->close(); // We return the file immediately after download return response()->download($zip_file);That’s it, nothing too difficult, right?
Archive all files in a folder storage/invoicesNothing changes from Laravel side, we will just add some more plain PHP code for iterating the files.