Home  >  Article  >  Backend Development  >  php ZipArchive setPassword is not work

php ZipArchive setPassword is not work

WBOY
WBOYOriginal
2016-08-20 09:04:172011browse

<code>$zip = new ZipArchive();
$code = $zip->open('myzip.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($code === true)
    echo 'zip opened<br>';
else
    echo $code.'<br>';
$zip->addFile('somefile');

$code = $zip->setPassword('secret');

if ($code === true)
    echo 'password set<br>';
else
    var_dump($code);

$code = $zip->close();

on the filesystem, the myzip.zip is created with somefile inside and $code is true but the zip file is not password protected ...
</code>

Reply content:

<code>$zip = new ZipArchive();
$code = $zip->open('myzip.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
if ($code === true)
    echo 'zip opened<br>';
else
    echo $code.'<br>';
$zip->addFile('somefile');

$code = $zip->setPassword('secret');

if ($code === true)
    echo 'password set<br>';
else
    var_dump($code);

$code = $zip->close();

on the filesystem, the myzip.zip is created with somefile inside and $code is true but the zip file is not password protected ...
</code>

setPassword is not to set a password for a ZIP file without a password. It is to set a password for decryption.

<code class="php">$zip = new ZipArchive();
$code = $zip->open('myzip.zip');
$zip->setPassword('123456');
$zip->extractTo('/path');</code>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn