我有一个导出一个 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);
}