php - 密码无法通过 PHP 在 Zip 文件中使用
问题描述
我正在创建有助于在提取或查看之前锁定 zip 的功能。在 StackOverflow 上所有与 zip 密码相关的线程的帮助下,我无法实现此输出。
从我的角度来看,我认为它是正确的。但是我无法理解的错误是什么以及为什么代码不起作用。低于我正在尝试的 - 请帮助我解决这个问题。
$dir = './';
$zip_file = 'file.zip';
// Get real path for our folder
$rootPath = realpath($dir);
// Initialize archive object
$zip = new ZipArchive();
$zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->setPassword('MY_PASSWORD');
// Create recursive directory iterator
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($rootPath),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
//Skip directories (they would be added automatically)
if (!$file->isDir()) {
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
$zip->setEncryptionName($zip_file, ZipArchive::EM_AES_256); //encrypt it
// Zip archive will be created only after closing object
$zip->close();
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($zip_file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($zip_file));
readfile($zip_file);
解决方案
推荐阅读
- java - 两个类具有相同的 XML 类型名称……”
- python-3.x - Discord Python:如何在事件下添加冷却命令
- git - 如何使用 Git Parameter 插件从多个存储库加载分支列表?
- html - 使用 CSS Flexbox 旋转 HTML 文本输入
- javascript - 为什么这 == 窗口?
- html - 如何在我的 SVG 示例中实现动画渐变
- java - Java:Antlr4 MySql 获取单个语句
- docker - docker run 什么都不做,没有创建日志
- docker - 在 docker 容器之间建立持久的 IP 连接
- linux - bash 脚本中的 z${variable} 和 zfalse 是什么意思?