Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.



Get Started Now!

Create Zip Files and Enable Download Functionality in Laravel 9

File compression and archiving are common tasks in web development, often required to bundle multiple files into a single, downloadable archive. In Laravel 9, a powerful PHP framework, achieving this functionality is straightforward using the ZipArchive class.

The code snippet you’ve provided is an example of how to create a zip file containing multiple files and offer it for download. Let’s dissect the code step by step.

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

    $fileName = 'myNewFile.zip';
 
    if ($zip->open(public_path($fileName), ZipArchive::CREATE) === TRUE)
    {
        $files = File::files(public_path('myFiles'));
 
        foreach ($files as $key => $value) {
            $relativeNameInZipFile = basename($value);
            $zip->addFile($value, $relativeNameInZipFile);
        }
           
        $zip->close();
    }
  
    return response()->download(public_path($fileName));
}

Here’s what each part of the code does:

$zip = new ZipArchive;: This line initializes a new ZipArchive object, which provides the functionality to work with zip archives.

$fileName = 'myNewFile.zip';: Here, you define the name of the zip file you want to create. You can customize this to suit your needs.

Opening the Zip Archive: The if condition checks if the zip archive can be opened for writing. If successful, it proceeds to add files to the archive.

Adding Files to the Zip Archive: Inside the loop, the code iterates through the files in the myFiles directory and adds each file to the zip archive. It uses addFile to specify the source file and the relative path within the archive.

Closing the Zip Archive: After adding all the files, the zip archive is closed using the close method.

return response()->download(public_path($fileName));: Finally, the code returns a response to download the created zip file. The public_path() function is used to specify the file path.

Usage Example

To use this code, you can create a route or controller method that invokes the code when a specific URL is accessed. For instance, if you want users to download a zip file by visiting example.com/download-zip, you can set up a route and controller for that URL.

Related Posts

Is SonarQube Community free Edition Good for Laravel Projects?

When working on web development projects using Laravel, JavaScript, and jQuery, maintaining code quality becomes just as important as building features. That’s where tools like SonarQube come…

Laravel Throttle Middleware: How to Increase API Rate Limit Safely and for 429 Too Many Requests

If you’re working with Laravel APIs, you might have encountered this default throttle setting: This line lives in your app/Http/Kernel.php file and controls how many requests a…

Fixing MySQL Error: Incorrect Definition of mysql.column_stats Table

The Problem While working on your MySQL server, you might come across this error in your error log: This error usually shows up after an upgrade or…

Fixing Laravel Migration Error: “Unknown Collation: utf8mb4_0900_ai_ci”

While working with Laravel and MySQL, you might run into an error during migrations like this one: Why This Happens The collation utf8mb4_0900_ai_ci is introduced in MySQL…

Why Dental Surgery Is Good and Important

Dental health plays a vital role in our overall well-being, yet it’s often overlooked until problems become serious. Dental surgery is a powerful solution that not only…

How to Get Cosmetic Surgery Covered by Insurance

Cosmetic surgery has become increasingly popular for individuals seeking to enhance their appearance or correct certain physical issues. While many people assume that cosmetic procedures are always…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x