微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP 7.2:如何使用PHP 7.2创建受密码保护的zip

如何解决PHP 7.2:如何使用PHP 7.2创建受密码保护的zip

谁能建议我们如何在PHP 7.2中创建受密码保护的zip

$zip = new \ZipArchive();

//Now working in PHP 7.2"
$zip->setPassword($password);

//also this is not creating protected zip

$zip->setEncryptionName($filename,ZipArchive::EM_AES_256,$password);

解决方法

从PHP 7.2开始,您应该能够使用setEncryptionName来加密每个文件的密码作为第三个参数,或者,如果先前曾调用过setPassword,则可以将其省略,并且可以使用默认密码。

直接来自documentation

从PHP 7.2.0和libzip 1.2.0开始,该密码用于解压缩存档,也是ZipArchive :: setEncryptionName()和ZipArchive :: setEncryptionIndex()的默认密码。以前,此功能仅设置用于解压缩归档文件的密码。并没有将不受密码保护的ZipArchive转换为受密码保护的ZipArchive。

下面的代码可以完成上述两种情况。 (有趣的是,打开该文件时,我的7-Zip似乎“记住”了上次输入的密码,因此在重新打开文件之前,我不会尝试同时输入这两个密码,但是我认为这是7-中的错误邮编。)

$zip = new ZipArchive;
if (!$zip->open('test.zip',ZipArchive::CREATE)) {
    die('Could not create zip file');
}

// Set the shared/default password
$zip->setPassword('cheese');

// Add a file with the default password
$zip->addFromString('test-1.txt','this is file one');
$zip->setEncryptionName('test-1.txt',ZipArchive::EM_AES_256);

// Add a file with a custom password
$zip->addFromString('test-2.txt','this is file two');
$zip->setEncryptionName('test-2.txt',ZipArchive::EM_AES_256,'custom password');
$zip->close();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。