Password Protected Zip File Creation in PHP
Tags: php
Published: 2024-04-22 17:18:51

---
title: Password Protected Zip File Creation in PHP
published: true
date: 2024-04-11 17:13:24 UTC
tags: PHP,php
canonical_url: https://larachamp.com/password-protected-zip-file-creation-in-php/
---

Hello readers, today we will learn about a little bit different topics. We will see how we can create a password-protected zip in PHP.

Since a few days back, I’ve been trying to write less theory, just to save time from both sides.

![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUwAAACYCAMAAAC4aCDgAAABqlBMVEX08PE2ubPi8u/8lgP+zgDg8+81urM6Ojoir6o3uLT17/Hj8vDh8+3k8/PW+Pfk9PS57evtmyTe1Ek2sayk497/lQA+s5onsqr/zQJqro3y8PPOoT4suLn47fH27+84s6P6uQvv8u+eqWQ0u67W0Tzw8uz4mAD09/v77O+cwmTv+/kAAAA1NDc9Oj04OzovMTC0vLrD4uh1xr/I7e6o2t6DxsdbuLOg2ddDsqpVsKbZ9PY3q50hIyWx39zO+/2U29WEioios7HR2diOzMLc8PmOy89Jro5buIl9w3avx2OntViopl2PqnJst3zy0SjboDe4p2KFqYXHzlD7rwiizVvDo1Hoshvknx5wrH/w1gvc2jS80VWVwHNkuonk1DZMrpeBrnqSqGNvpZXs0Du6qkZbsoqCwWrjnjYut8GauWerplH0lSGgq1zSpTHLrii2jiG5exm5oStJMxgwHy8TCSIiABeIXiAgIC06N0cdJhsCHhl5Th44MB8ADBkSHyWVfBtfUSAoEhFIPh45PjPcwj6rbzE2JypAIBRiwLF2fHxeYWJMTU+UnJt6gH4Wl6FXAAAVAUlEQVR4nO2diWPbxpXGAUoccgCKkExJkBEADIyQoGEeMmmRMAlZEuUrG0fJuknjbJqjmzrxbjbdJL3iNnW3m6SNJfl/7nsD3gR4SLIZUP5kySSOkfDjm3nvzQwGHPezkSbAD4ETNJ1TmLzNelf4ThC8fTo7HE4QZvYH/+wlaBqS8tApFsiWJCkahR82vlNadIG3piHPlzADJQgG/qcJwLB+9dqtcrVKaYqJ0mq1fOva1TpQZQ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qFj7PyeC0c1fDUZAJwbWDW7du7jdOnl/iPJHGvmRxGQNjhHOS9wxK0TQNPC9WTaW+k057Xb2ETaIZj5DVbR67g9POTh0y8ZwOUQIUeY5ynwDluORqNSWTzgKOo/uC24tE8kSOV1eTIb+L4mylKOCKNLuyX43jwpjMQROfgKk1tMNg8rgQbLy6X7E1LcQLb5y9oJ3LgHTIMbccWUaT6wyW9d2y197OboWUna2Kpel45jn1Or4SNJzbBQ2oolh2pVZ2UvLgCj691RwynZRTrlUsC04QwC6hgJcwOxJAmgaendOBiyVValtVRx5amRytNC471f3timTBgQKeobEPYdZX8DMT8mjFNZBeW7YUBaTlatUBO5XjqZTjVKvl/VolGrUtRWi5HDzhJchJZHAWSpKkel2Slmx885Lc9GImqkMtNlpDOQZLbwwjo3M/3wGJfwHpcBxnQ+1GeAAAAABJRU5ErkJggg==)

## Create A PHP File

Yes, you read it right, we just need a single PHP file to create a password-protected zip file. Let’s see how.

Use the below code :

```
<?php

$password = 'password';
$zipFileName = 'protected';
$filesToZip = ['test.py', 'test.txt'];
shell_exec('zip -jrq -P ' . $password . ' ' . $zipFileName . '.zip ' . 'test.py');

//for multiple files use space between file name 
shell_exec('zip -jrq -P ' . $password . ' ' . $zipFileName . '.zip ' . 'test.txt test.py');
```

## Code Description

- `zip`: This is the command-line tool used to create zip archives.
- `-j`: This flag tells `zip` to store just the file, not its directory structure. It’s useful if you want to add files from different directories without retaining their original directory structure.
- `-r`: This flag stands for “recursive” and includes all files and directories within the specified directory.
- `-q`: This flag stands for “quiet” and suppresses the output of the `zip` command, making the process less verbose.
- `-P`: This flag specifies the password for the zip file.
- `$password`: This is the variable containing the password for the zip file.
- `$zipFileName . '.zip'`: This is the name of the zip file you want to create. The `.zip` extension is appended to the variable `$zipFileName`.
- `'test.py'`: This is the file or directory you want to add to the zip archive.

So, in summary, the command is creating a zip archive named `$zipFileName.zip` (with the password specified by `$password`) and adding the file `test.py` to it, while ignoring the directory structure (`-j` flag) and including all files and directories within `test.py` (`-r` flag).

Suggested: [https://larachamp.com/boost-your-productivity-with-essential-phpstorm-shortcuts/](https://dev.to/robinkashyap_01/boost-your-productivity-with-essential-phpstorm-shortcuts-3e67)

For Laravel: [Creating Password-Protected Zip Files In Laravel](https://medium.com/@arjunamrutiya/creating-password-protected-zip-files-in-laravel-a-step-by-step-guide-dd7ba3b9920a)

## Conclusion

Implementing password protection for zip files in PHP is now simple and secure. Customize the code as needed for dynamic file lists or user authentication.

The post [Password Protected Zip File Creation in PHP](https://larachamp.com/password-protected-zip-file-creation-in-php/) appeared first on [Larachamp](https://larachamp.com).