Diana
Diana

Reputation: 31

How to set password in zip archive with php

I just want to set password in already existing zip archive, I use method $zip->setPassword("MySecretPassword") , The function returns true, but password is absent. After code running I can extract files without puting password. Here is my code like this:

<?php
    $zip = new ZipArchive();
    $zip_status = $zip->open("test.zip");

    if ($zip_status === true)
    {
        if ($zip->setPassword("MySecretPassword"))
        {
            if (!$zip->extractTo(__DIR__))
                echo "Extraction failed (wrong password?)";
        }

        $zip->close();
    }
    else
    {
        die("Failed opening archive: ". @$zip->getStatusString() . " (code: ". $zip_status .")");
    }
?>

Can any one help me.

Upvotes: 2

Views: 1153

Answers (1)

Scuzzy
Scuzzy

Reputation: 12332

http://php.net/manual/en/ziparchive.setpassword.php

This function only sets the password to be used to decompress the archive; it does not turn a non-password-protected ZipArchive into a password-protected ZipArchive.

The top comment also says

Wouldn't it make sense for this method to be named ZipArchive::usePassword, instead? There seems to be a lot of people thinking that its name, currently (ZipArchive::setPassword), is for applying a password. I think nomenclature should certainly be up for discussion on this method.

This would suggest that this function is for setting the password when performing extraction only using the ZipArchive class.

Upvotes: 1

Related Questions