Create ZIP file with password – PHP

To create a ZIP file with password in PHP, there isn’t any need for third-party library. PHP provides a large variety of built-in functions to create a ZIP file. Also PHP has an in-built option for setting the password on ZIP files.

We will creating a simple script to allow user to upload multiple files, then will compress those files in ZIP and asking the password from user. And apply that password on that ZIP file.

We will be creating just 2 files, 1 for client (index.php) and 1 for server (upload.php). So, start off by creating a simple HTML form in file index.php:

Create form to select multiple files (index.php)

<form method="POST" enctype="multipart/form-data" action="upload.php">
</form>

enctype=”multipart/form-data” and method=”POST” are important in <form> tag to upload file.

<input type="file" name="files[]" required multiple />
<input type="submit" name="upload" />

multiple attribute will be used to allow user to select multiple files.

Upload, compress files to ZIP (upload.php)

<?php
    $zip = new ZipArchive();
?>

Creates a built-in ZipArchive class object and save it in variabled called $zip.

Call open() function from $zip object to create a new empty ZIP file in memory.

$zip->open("file.zip", ZIPARCHIVE::CREATE);
  • First parameter is the name of ZIP file you want to set.
  • Second is the action you want to perform, you can use OPEN, CREATE methods to read or write the file respectively.

As we are allowing user to select multiple files, we need to create a loop on user selected files:

for ($a = 0; $a < count($_FILES["files"]["name"]); $a++)
{
    //
}

Inside this loop, we have to get the content of each file separately:

$content = file_get_contents($_FILES["files"]["tmp_name"][$a]);

Then you have to call the addFromString() method on $zip object. It accepts 2 parameters, 1st is the name of file inside the ZIP archive, 2nd is the content of file. In this case, 2nd parameter is stored in variable called $content.

$zip->addFromString($_FILES["files"]["name"][$a], $content);

After the loop, we have to call the close() to free-up the space in memory.

$zip->close();

At this point, your index.php file will look like this:

<form method="POST" enctype="multipart/form-data" action="upload.php">
    <input type="file" name="files[]" required multiple />
    <input type="submit" name="upload" />
</form>

And your upload.php file will look like this:

<?php
    $zip = new ZipArchive();
    $zip->open("file.zip", ZIPARCHIVE::CREATE);
    for ($a = 0; $a < count($_FILES["files"]["name"]); $a++)
    {
        $content = file_get_contents($_FILES["files"]["tmp_name"][$a]);
        $zip->addFromString($_FILES["files"]["name"][$a], $content);
    }
    $zip->close();
?>

Applying password to archive (upload.php)

Create simple password input field in index.php file:

<input type="password" name="password" required />

And in upload.php file, after the open() method, call the setPassword() method and send user selected password as parameter:

$zip->setPassword($_POST["password"]);

And in loop, after addFromString() method, call setEncryptionName() method to apply password on each file in the ZIP:

$zip->setEncryptionName($_FILES["files"]["name"][$a], ZipArchive::EM_AES_256);
  • 1st parameter is the name of file.
  • 2nd parameter is the type of encryption algorithm used.

Below is the complete source code of both files:

index.php

<form method="POST" enctype="multipart/form-data" action="upload.php">
    <input type="file" name="files[]" required multiple />
    <input type="password" name="password" required />
    <input type="submit" name="upload" />
</form>

upload.php

<?php
    $zip = new ZipArchive();
    $zip->open("file.zip", ZIPARCHIVE::CREATE);
    $zip->setPassword($_POST["password"]);

    for ($a = 0; $a < count($_FILES["files"]["name"]); $a++)
    {
        $content = file_get_contents($_FILES["files"]["tmp_name"][$a]);
        $zip->addFromString($_FILES["files"]["name"][$a], $content);
        $zip->setEncryptionName($_FILES["files"]["name"][$a], ZipArchive::EM_AES_256);
    }
    $zip->close();
?>

Run the index.php file, upload some files and set the password. Your selected files will be archived to 1 and password will be applied.

If you want to ZIP your files and protect them with password in command line, follow here.

Right now, this only works for Mac OS X.

[wpdm_package id=’107′]

2 Replies to “Create ZIP file with password – PHP”

  1. Link exchange is nothing else but it is just placing the other person’s website link on your page at appropriate
    place and other person will also do same in favor of you.

Comments are closed.