Reputation: 7954
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
Upvotes: 0
Views: 4311
Reputation: 1035
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);
}
}
Upvotes: 3
Reputation: 197787
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:
Upvotes: 1