Im使用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在压缩文件中
这里有一个简单的代码,可以将csv压缩并输出为压缩文件
//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);
}
}
我只想知道是否可以在zip文件中下载csv文件
是的,这是可能的。只需创建一个ZIP文件并将CSV文件放在其中即可。然后交付ZIP文件。
如果您正在寻找更多技术信息,请阅读相关问答;作为具体代码的材料在你需要实现的内容上有很大的不同:
- 用PHP+Apache快速生成ZIP文件