Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Laravel

How to Create and Download Zip File in Laravel 11

  • 21 Feb, 2025
  • Com 2
How to Create and Download Zip File

Laravel has filesystem utility classes by default, but creating a zip might require using PHP’s native ZipArchive class. So, the basic steps would be to create a new ZipArchive instance, open it, add files to it, close it, and then return a response to download the zip.

Here is the step-by-step guide:

Step 1: Install Laravel

composer create-project laravel/laravel zip-app

Step 2: Create a controller file

php artisan make:controller ZipperController

Add the folder inside public >> images and add images

images_folder

Step 3: Create a new Controller

Add the below code inside the ZipperController.php file.

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use File;
use ZipArchive;

class ZipperController extends Controller
{

  public function __invoke()
  {
    $zip = new ZipArchive;

    $fileName = 'data.zip';

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

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

      $zip->close();
    }

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

Step 4: Create a Route inside the web.php file

Add the below code inside the routes >> web.php file.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ZipperController;

Route::get('/', function () {
    return view('welcome');
});

Route::get('download-zip-file', ZipperController::class);

Step 5: Run the project

Go to the project’s root and start the Laravel server using this command:

php artisan serve

Now, go to this command: http://localhost:8000/download-zip-file

You will see that it will download the data.zip file.

zip_file

That’s it!

Post Views: 81
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Get the Length of an Array in Python
How to Find the Length of a String in Python

2 Comments

  1. akshay

    June 12, 2018 at 12:04 pm

    I have used chumper/zipper package the code works fine but when i download the zipped archive i am getting no archive found. is there any solution to this or any other package. Suggestions would be helpful,thank you.

    Size of zipped files: https://pasteboard.co/Hpz00p7.png

    afterdownloading: https://pasteboard.co/HpyYDQ3.png

    Reply
  2. Mike

    June 22, 2018 at 10:15 pm

    I enjoy the article

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend