2017-06-22 122 views
0

我正在研究一個Web應用程序,該應用程序詢問一系列問題,然後從這些問題中生成文本文檔。該文檔已創建,並且客戶端瀏覽器會自動觸發下載。這非常完美。創建一個包含動態生成的文件的ZIP文件

但是,我現在想要做的是從這個文件創建一個ZIP文件,當它動態創建並添加一些額外的文件也。雖然我無法弄清楚這是否可以用PHP?

任何人都可以建議嗎?我環顧四周,但所有示例/教程都演示瞭如何從文件系統中已存在的文件創建ZIP文件?

謝謝。

+0

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

+0

你的一個紳士,我設法從這個工作,並得到它的工作,我想如何: ) – CrispyDuck

+0

對於你的信息,你可以使用密碼保護它:'$ zip-> setPassword(「YourPasswordHere」);' – Richard

回答

1

因此,對於那些誰感興趣的話,我用下面的代碼從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); 

相應調整了我的要求和它的作品。