How To Create Zip File In Laravel 7/8

Websolutionstuff | Dec-17-2021 | Categories : Laravel

In this tutorial I will give you example of how to create zip file in laravel 7/8. Some times client's have requirments to have functionalities like create zip file for documantation or images and download it. So, using ziparchive function you can create zip file and download in laravel 7/8.

In this example I will show you to how to create zip file in laravel using ziparchive without any packege. Laravel provide ZipArchive class for create zip file in laravel,So I will use ZipArchive in laravel 7/8.

Read More Official Document of PHP : ZipArchive

In below code I have created one function in laravel controller and added ZipArchive class.

Note : I have created  ZipArchive_Example folder in public folder and added some images. So, you need to also create one folder and  add some file also.

 

Step 1: Add Route

In this step we can add route for create and download zipfile. So, add below code in web.php file.

routes/web.php


<?php

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

Route::get('ziparchive_example', [ZipFileController ::class, 'ZipArchiveExample']);

 

 

Step 2 : Create Controller

Now, create controller and add function ZipArchiveExample.

app/Http/Controllers/ZipFileController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use ZipArchive;

class ZipFileController extends Controller
{
    public function ZipArchiveExample()
    {                                
        $zip = new ZipArchive;

        $fileName = 'Zipfile_Example.zip';

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

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

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

Now run below command in your terminal.

php artisan serve

 

 

Now you can open bellow URL on your browser:

http://localhost:8000/ziparchive_example

 


You might also like :

Recommended Post
Featured Post
How To Install Python On Ubuntu
How To Install Python On Ubunt...

In this article, we will see how to install python on ubuntu. Python is a popular programming language. P...

Read More

May-06-2022

Carbon Add Minutes To Date In Laravel 9
Carbon Add Minutes To Date In...

In this article, we'll explore how to add minutes to a date in Laravel 8, Laravel 9 and Laravel 10 using Carbon...

Read More

Nov-23-2022

How To Convert PHP Array To JSON Object
How To Convert PHP Array To JS...

In this article, we will explore the process of converting a PHP array into a JSON object. We'll achieve this transf...

Read More

Jul-08-2020

Generate Dynamic Sitemap in Laravel
Generate Dynamic Sitemap in La...

Here we will see how to generate dynamic sitemap in laravel. As we know sitemap is very important part of seo, sitemap i...

Read More

Jul-05-2021