我有一个导出功能,一个csv文件包含每个调查的所有答案。我需要一个函数将所有调查的所有答案(每个csv文件一个调查)导出到menory中并在下载到计算机之前压缩它
不将所有csv文件保存到服务器或本地存储中,我需要将其存储到内存中并在下载到计算机之前将其压缩
我使用过这段代码:
function exportAllAnswer() {
allTokenId = getAllTokenId();
foreach(allTokend as $key->$value) {
exportAnswer($value->id, false);
}
$path = 'exportAllCsv';
// we deliver a zip file
header("Content-Type: archive/zip");
// filename for the browser to save the zip file
header("Content-Disposition: attachment; filename=allCsvs.zip");
// get a tmp name for the .zip
$tmp_zip = tempnam ("tmp", "tempname") . ".zip";
//change directory so the zip file doesnt have a tree structure in it.
chdir($path);
// zip the stuff (dir and all in there) into the tmp_zip file
exec('zip '.$tmp_zip.' *');
// deliver the zip file
$fp = fopen("$tmp_zip","r");
echo fpassthru($fp);
// clean up the tmp zip file
unlink($tmp_zip);
// delete folder csv
rmdir($path);
}