Creating Password-Protected Zip Files in Laravel: A Step-by-Step Guide

2 min readDec 7, 2023

When dealing with sensitive data in Laravel applications, ensuring security is crucial. One effective way to enhance the security of your files is by creating password-protected zip archives. In this guide, we’ll walk you through the process of implementing password protection for your zip files using Laravel’s built-in ZipArchive class, without relying on external packages.


Before we begin, make sure you have the following prerequisites:

  • Laravel installed on your machine.
  • Basic understanding of Laravel’s file handling.

Step 1: Set Up a Laravel Project

If you haven’t already, create a new Laravel project using the following command:

composer create-project --prefer-dist laravel/laravel password-protected-zip

Step 2: Create a Controller

Generate a new controller using the Artisan command:

php artisan make:controller ZipController

Step 3: Implement Password-Protected Zip Creation

Open the ZipController and add the following code:


namespace App\Http\Controllers;
use ZipArchive;

class ZipController extends Controller
public function createPasswordProtectedZip()
$filesToZip = [ public_path('files/file1.txt'), public_path('files/file2.txt'), // Add more files as needed ];
$zip = new ZipArchive();
$zipFileName = public_path('');
$password = 'your_secret_password';
if ($zip->open($zipFileName, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
foreach ($filesToZip as $file) {
$zip->addFile($file, basename($file));
return response()->download($zipFileName)->deleteFileAfterSend(true);
} else {
return response()->json(['error' => 'Failed to create the zip file.']);

Step 4: Define Routes

In the routes/web.php file, define a route that calls the createPasswordProtectedZip method:

use App\Http\Controllers\ZipController;

Route::get('/create-zip', [ZipController::class, 'createPasswordProtectedZip']);

Step 5: Test Your Implementation

Run the Laravel development server:

php artisan serve

Visit http://localhost:8000/create-zip in your browser. This will trigger the creation of a password-protected zip file containing the specified files.


Congratulations! You’ve successfully implemented password protection for your zip files in Laravel using the ZipArchive class. This extra layer of security ensures that only authorized users can access the contents of your compressed files.

Feel free to customize the code to suit your specific requirements, such as dynamically generating file lists or handling user authentication before allowing access to the protected zip files. Enhancing security in your Laravel applications has never been easier!

Go forth and get more out of your content. Go forth and conquer Medium! (and while you’re at it, follow me on Medium! and feel free to Subscribe)

Found this post useful? Kindly tap the 👏 button below! :)

🌟 Enjoy my blogging content? Support my work by buying me a virtual coffee on BuyMeACoffee! Your contributions help fuel insightful tech articles. Join hands in making coding more accessible and exciting for all.🚀




👋 Hey there! I'm Arjun Amrutiya, a passionate web developer and blogger who loves all things PHP, Laravel and Vue.js. Welcome to my Medium account!