下载csv作为zip

时间:2013-08-30 10:47:08

标签: php javascript zend-framework csv zip

我使用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.I希望csv位于zip文件中

2 个答案:

答案 0 :(得分:3)

这是一个简单的代码,用于压缩和输出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);
    }   
}

答案 1 :(得分:1)

  

我只想知道是否可以在zip文件中下载csv文件

是的,这是可能的。只需创建一个ZIP文件并将CSV文件放在那里。然后发送ZIP文件。

如果您正在寻找更多技术信息,请阅读相关的问答材料,因为具体代码与您需要实现的内容有很大差异:

相关问题