Laravel

Create multiple Zip Files and Download in Laravel application

In Laravel, you can create multiple zip files and download them using the built-in “ZipArchive” class and the “File” facade.

  1. Use the “File” facade to create the zip files. For example:
use File;

class MyController extends Controller {
    public function download() {
        $files = [
            public_path('file1.txt'),
            public_path('file2.txt'),
            public_path('file3.txt'),
        ];

        $zipName = 'files.zip';
        $zip = new ZipArchive;
        $zip->open($zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE);

        foreach ($files as $file) {
            $zip->addFile($file);
        }

        $zip->close();
    }
}

2. Use the “response()->download()” method to send the zip file to the user. For example:

class MyController extends Controller {
    public function download() {
        $files = [
            public_path('file1.txt'),
            public_path('file2.txt'),
            public_path('file3.txt'),
        ];

        $zipName = 'files.zip';
        $zip = new ZipArchive;
        $zip->open($zipName, ZipArchive::CREATE | ZipArchive::OVERWRITE);

        foreach ($files as $file) {
            $zip->addFile($file);
        }

        $zip->close();

        return response()->download($zipName);
    }
}
Anil Bajracharya

Developer,a tech enthusiastic who's passionate about learning new technology and programming Find me on linkedin linkedin

Share
Published by
Anil Bajracharya

Recent Posts

Bisket jatra highlight

भक्तपुरमा नौ दिन आठ रातसम्म परम्परागत विधिअनुसार मनाइने विश्वप्रसिद्ध बिस्का: जात्रा आज चैत २७ गते…

3 weeks ago

Viennese Jewish – The Woman Who Invented WI-FI

The Woman Who Invented WI-FI. The most beautiful woman in the world in cinema and…

3 months ago

Sadhguru Quotes in nepali

“यस क्षण” कुनै सोच होइन । यो एक मात्र वास्तविकता हो तपाईं आफूलाई अरूसँग तुलना…

3 months ago

how to check See result with marksheet

Dear SEE Students: तपाईंको नतिजाको Grading system यस्तो रहनेछ ! कुनै पनि विद्यार्थी फेल हुने…

10 months ago

10 essential prompts to boost your

10 essential prompts to boost your productivity: 1. Summarize: "Summarize the text below into 500…

11 months ago

15 powerful ChatGPT prompts to save you 15 hours a week

15 powerful ChatGPT prompts to save you 15 hours a week: 1. Use the 80/20…

11 months ago

This website uses cookies.