下载的目录结构,而无需将其保存在服务器上


directory structure for download without to save it on server

我有一个项目,我想创建一个动态目录树结构,而无需将其保存在服务器上,压缩并推送下载。有可能做到这一点吗?

您可能正在寻找类似ZipArchive::addEmptyDir的东西,它可以用于在ZIP归档中创建空目录。以后可以将文件添加到该目录中。

您仍然需要将生成的存档保存到磁盘。。。

$file = tempnam("tmp", "zip");
$zip = new ZipArchive();
$zip->open($file, ZIPARCHIVE::CREATE);
$zip->addEmptyDir('newDirectory');
$zip->addFromString('newDirectory/demo.txt', 'file contents');
$zip->close();
// Stream the file to the client 
header('Content-Type: application/zip'); 
header('Content-Length: ' . filesize($file)); 
header('Content-Disposition: attachment; filename="some_archive_file.zip"'); 
readfile($file); 
unlink($file);

如果您希望在不保存存档的情况下流式传输存档,您可以尝试查看PHPZip类

您可以将passthru与命令行归档实用程序组合使用,即:

passthru("tar dir0/ dir1/ dir2/dir23/dir234/ dir4/file4.txt | gzip -f");