Laravel 9 Create Zip File And Download

Websolutionstuff | May-02-2022 | Categories : Laravel PHP

In this article, we will see laravel 9 create a zip file and download it. Laravel provides ZipArchive class for creating zip files in laravel 9. I will give you examples step by step of how to create a zip file from a folder and download it in the laravel 9 application. we will create a zip file using the zip archive class in PHP laravel 9 application.

So, let's see how to create zip file and download in laravel 9, ziparchive laravel 9, create zip file in laravel 9, create a zip file from a folder and download in laravel 9, create zip file using ziparchive in laravel 9.

Step 1: Create Route

In this step, we will create a route in the web.php file

<?php
  
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\ZipArchiveController;
  
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
  
Route::get('download-zip', [ZipArchiveController::class, 'downloadZip']);

 

 

Step 2: Create Controller

Now, create a new controller name as ZipArchiveController.

app/Http/Controllers/ZipArchiveController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use ZipArchive;

class ZipArchiveController extends Controller
{
    public function downloadZip()
    {                                
        $zip = new ZipArchive;

        $fileName = 'Example.zip';

        if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
        {
            $files = \File::files(public_path('Zip_Example'));

            foreach ($files as $key => $value) {
                $file = basename($value);
                $zip->addFile($value, $file);
            }
             
            $zip->close();
        }

        return response()->download(public_path($fileName));
    }
}

Note: I have created a Zip_Example folder in the public folder and added some images. So, you need to also create one folder and add some files also.

Output:

http://localhost:8000/download-zip

 


You might also like :

Recommended Post
Featured Post
How to Install PHP Soap Extension in Ubuntu 23.04
How to Install PHP Soap Extens...

Hey fellow developers! Today, let's tackle the installation of the PHP SOAP extension on our Ubuntu 23.04 systems. I...

Read More

Jan-31-2024

How To Store Backup On Dropbox In Laravel 9
How To Store Backup On Dropbox...

In this article, we will see how to store the backup on dropbox in laravel 9. Here, we will learn to store database...

Read More

Jan-16-2023

How To Upload Multiple Image In Laravel 9
How To Upload Multiple Image I...

 In this article, we will see how to upload multiple images in laravel 9. here, we will see the tutorial of multipl...

Read More

Mar-18-2022

How To Count Working Days In Laravel 9
How To Count Working Days In L...

In this article, we will see how to count working days in laravel 9. Here, we will learn to calculate working days...

Read More

Jan-23-2023