在PHP中从文件流创建Zip

3
我已经在内存中创建了多个 PDF 文件流。我能否从这些流创建一个 zip 文件,如果可以,应该如何操作?我想让用户下载我的文件的 zip 文件,而不在服务器上创建任何文件。 ZipArchive 似乎只在运行 addFile 时使用文件名。

1
听起来你想使用 https://secure.php.net/manual/zh/class.ziparchive.php#ziparchive.addfromstring. - user6763587
1个回答

0

我最终在这里找到了解决方案:

与那里给出的解决方案类似,我使用了来自phpmyadmin的类,如下所示:

header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename='whatever.zip'");

$zip = new ZipFile;
foreach($pdfs as $idx => $content) {
    $zip->addFile($content, "file$idx.pdf");
}
file_put_contents("php://output", $zip->file());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接