How to Create Zip File and Download in Laravel

How to Create Zip File and Download in Laravel

In this post we will give you information about How to Create Zip File and Download in Laravel. Hear we will give you detail about How to Create Zip File and Download in Laravel And how to use it also give you demo for it if it is necessary.

It is preferable to make one archive and allow users to download it if you require them to be able to download many files at once. The laravel method is shown here.

Actually, we will be utilizing the ZipArchive class, which has been there since PHP 5.2, It’s less about Laravel and more about PHP. To utilize it, confirm that the ext-zip extension is enabled in your php.ini.

How to Create Zip File and Download in Laravel

Here’s the code:

<?php

namespace AppHttpControllers;

use IlluminateHttpRequest;

class ZipController extends Controller
{
    public function downloadZip(Request $request){
        $zip_file = 'invoices.zip';

        // Initializing PHP class
        $zip = new ZipArchive();
        $zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);

        $invoice_file = 'invoices/AMZ-001.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.

Thank you for reading this article.

Also see: How To Count Days Between Two Dates in Laravel

  .       .

Hope this code and post will helped you for implement How to Create Zip File and Download in Laravel. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US