简体   繁体   中英

Downloading a csv as a zip

Im using Php (Zend Framework) to download a datagrid as CSV..the csv generation works fine.I just want to know is it possible to download the csv file within a zip file

My current code

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;

}

By calling the url controller/downloadCsv im getting a popup to save the csv.I want the csv to be in a zip file

Here is a simple code to zip and output csv as a zip file

//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);
    }   
}

I just want to know is it possible to download the csv file within a zip file

Yes it is possible. Just create a ZIP-file and put the CSV-file in there. Then deliver the ZIP-file.

If you're looking for more technical info, please read into the related Q&A material as the concrete code differs highly on what you need to achieve:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM