如何在没有中间服务器端文件的情况下在 PHP 中创建和下载 zip 存档


How to create and download a zip archive in PHP without an intermediary server-side file

PHP ZipArchive 类将允许我使用 open() 方法创建一个 zip 存档:

mixed ZipArchive::open ( string $filename [, int $flags ] )
然后,我可以将文件

添加到此存档中,关闭文件,然后像往常一样将其下载到用户的Web浏览器中:

header ('Content-Type: application/zip');
header ('Content-Length: ' . filesize ($filename));
header ('Content-Disposition: attachment; filename="' . $filename . '"');
readfile ($filename);
unlink ($filename);

我的问题:有没有办法避免在服务器硬盘上创建文件,下载该文件然后删除它? 即是否可以使用 stdout 作为传递给 open() 方法的目标文件名并将存档内容直接写入 stdout?大致如下:

ZipArchive::open ( STDOUT, ZipArchive::CREATE)

还是我坚持需要中间服务器端文件?

"ZipStream 是一个库,用于从 PHP 动态流式传输动态 zip 文件,而无需在 服务器。链接: https://github.com/maennchen/ZipStream-PHP