Reputation: 33
I am building an online app to take a selection of variables from multiple (unrelated) datasets and build output csv files for each of the datasets that have been selected (only containing the selected variables, not all available variables).
It is almost working except that I end up with only one csv file, and it only contains the variables from the last of the selected datasets.
I am trying to use $this->set to pass the data to an output ctp view by looping through an array of the selected datasets (properly nested to allow easy use of foreach loops). I can sort of get it why I end up with just one csv, but does anyone have any ideas how this might be implemented correctly in cakephp?
This is the ctp view used to generate the csv file.. it works for one dataset.
<?php
ini_set('max_execution_time', 600); //increase max_execution_time to 10 min if data set is very large
//create a file
$filename = "export_".date("Y.m.d").".csv";
$csv_file = fopen('php://output', 'w');
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename="'.$filename.'"');
//loop through data array passed by $this->set in controller
foreach ($end_values as $row)
{
fputcsv($csv_file,$row,',','"');
}
fclose($csv_file);
?>
I have altered the $filename to include a reference to the individual dataset (just using a session variable for now) - so for each pass, the filename will be different:
$filename = "export_".$this->Session->read('dataset_tag')."_".date("Y.m.d").".csv";
But still get the same problem - only the final dataset csv is created..
Upvotes: 0
Views: 1321
Reputation: 7575
You can not stream more than one file at a time to the user. You should generate each csv file, write to disk then create a archive (tar, zip etc) and send them the archive.
Upvotes: 1