我想创建5个不同的文件,用于存储数据库中的数据。我想压缩5个文件并让此函数返回zip。
我是否可以创建5个文件而无需将它们实际写入磁盘?我从db获得的数据只是字符串,因此每个文件都是一个长字符串。
我只想这样做:
function getZippedFiles()
// Create 1..5 files
// Zip them up
// Return zip
end
main()
// $zip_file = getZippedFiles();
end
非常感谢有关如何做到这一点的任何信息,谢谢!
答案 0 :(得分:1)
当然可以, ZipArchive
非常简单// What the array structure should look like [filename => file contents].
$files = array('one.txt' => 'contents of one.txt', ...);
// Instantiate a new zip archive.
$zip_file = new ZipArchive;
// Create a new zip. This method returns false if the creation fails.
if(!$zip_file->open('directory/to/save.zip', ZipArchive::CREATE)) {
die('Error creating zip!');
}
// Iterate through all of our files and add them to our zip stream.
foreach($files as $file => $contents) {
$zip_file->addFromString($file, $contents);
}
// Close our stream.
$zip_file->close();