创建一个包含动态生成的文件的ZIP文件

问题描述:

我正在研究一个Web应用程序,该应用程序询问一系列问题,然后从这些问题中生成文本文档。该文档已创建,并且客户端浏览器会自动触发下载。这非常完美。创建一个包含动态生成的文件的ZIP文件

但是,我现在想要做的是从这个文件创建一个ZIP文件,当它动态创建并添加一些额外的文件也。虽然我无法弄清楚这是否可以用PHP?

任何人都可以建议吗?我环顾四周,但所有示例/教程都演示了如何从文件系统中已存在的文件创建ZIP文件?

谢谢。

+0

看一看https://*.com/questions/1061710/php-zip-files-on-the-fly – Richard

+0

你的一个绅士,我设法从这个工作,并得到它的工作,我想如何: ) – CrispyDuck

+0

对于你的信息,你可以使用密码保护它:'$ zip-> setPassword(“YourPasswordHere”);' – Richard

因此,对于那些谁感兴趣的话,我用下面的代码从SO提到上面张贴:

// Prepare File 
$file = tempnam("tmp", "zip"); 
$zip = new ZipArchive(); 
$zip->open($file, ZipArchive::OVERWRITE); 

// Stuff with content 
$zip->addFromString('file_name_within_archive.ext', $your_string_data); 
$zip->addFile('file_on_server.ext', 'second_file_name_within_archive.ext'); 

// Close and send to users 
$zip->close(); 
header('Content-Type: application/zip'); 
header('Content-Length: ' . filesize($file)); 
header('Content-Disposition: attachment; filename="file.zip"'); 
readfile($file); 
unlink($file); 

相应调整了我的要求和它的作品。