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

We spend hours scrolling social media and waste money on things we forget, but won’t spend 30 minutes a day earning certifications that can change our lives.
Master in DevOps, SRE, DevSecOps & MLOps by DevOps School!

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

How to Generate a GitHub OAuth Token with Read/Write Permissions for Private Repositories

When working with GitHub, you may need to interact with private repositories. For that, GitHub uses OAuth tokens to authenticate and authorize your access to these repositories….

Laravel Error: Target class [DatabaseSeeder] does not exist – Solved for Laravel 10+

If you’re working with Laravel 10+ and run into the frustrating error: …you’re not alone. This is a common issue developers face, especially when upgrading from older…

JWT (JSON Web Token) vs OAuth 2.0

Both JWT and OAuth 2.0 are used for managing authentication and authorization, but they serve different purposes and work in distinct ways. 1. Purpose: 2. Role: 3….

Exploring and Creating a Proof of Concept (POC) to Upload APK Directly from GitHub Package

Automating the process of uploading an APK (or AAB) to the Google Play Store from GitHub can significantly speed up your CI/CD pipeline. By integrating Google Play’s…

A Detailed Guide to CI/CD with GitHub Actions

Continuous Integration (CI) and Continuous Deployment (CD) are modern software development practices that automate the process of integrating code changes, running tests, and deploying applications. With the…

Step-by-Step Guide for Setting Up Internal Testing in Google Play Console

1. Understanding the Types of Testing Before uploading your Android app for internal testing, it’s essential to know the differences between the testing options available in Google…

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