我使用 Php(Zend Framework)将数据网格下载为 CSV。.csv 生成工作正常。我只是想知道是否可以在 zip 文件中下载 csv 文件
我当前的代码
public function downloadCsvAction(){
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=answers_csv.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo "stuff1" . ",";
echo "stuff2".",";
//----bla bla
exit;
}
通过调用 URL controller/downloadCsv
,我会弹出一个保存 csv 的弹出窗口。我希望 csv 位于 zip 文件中
最佳答案
这是一个简单的代码,用于将 csv 压缩并输出为 zip 文件
//here is your csv file
$csvFile = "test.csv";
//location for php to create zip file.
$zipPath = "/tmp/$csvFile.zip";
$zip = new ZipArchive();
if ($zip->open($zipPath,ZipArchive::CREATE)) {
$zip->addFile($csvFile, $csvFile);
$zip->close();
//Make sure the zip file created and output it.
if(is_file($zipPath)){
header('Content-type: application/octet-stream; charset=utf-8');
header('Content-Disposition: attachment; filename="'.$csvFile.'.zip"');
header('Content-Length: ' . filesize($zipPath));
readfile($zipPath,false);
}
}
关于php - 将 csv 下载为 zip 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18531034/