2

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.

Feras Al Sous
  • 1,073
  • 1
  • 12
  • 23
Diana
  • 31
  • 1
  • 1
    Possible duplicate of [ZIP a file and protect with a password in PHP](https://stackoverflow.com/questions/39833496/zip-a-file-and-protect-with-a-password-in-php) – mseifert Oct 30 '17 at 07:42

1 Answers1

1

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.

Scuzzy
  • 12,186
  • 1
  • 46
  • 46
  • thank you Scuzzy, but if (!$zip->extractTo(__DIR__)) is false, that's mean that password is not setting in decompress. After code running test.zip - is extracted – Diana Oct 30 '17 at 08:00