I have a large large table of data (in mongodb) with more than 5 million rows. I want to export the data to csv and output the csv in browser directly. I am using PHP.
For smaller record set less than a million rows, the following code works perfectly.
set_time_limit(0);
$output = fopen("php://output",'w') or die("Can't open php://output");
header("Content-Type:application/csv");
header("Content-Disposition:attachment;filename=auditresults_$i.csv");
fputcsv($output, array('id','createdon','colum1','column2','column3','colum4'));
foreach ($items as $item) {
fputcsv($output, $item)
}
fclose($output) or die("Can't close php://output");
However, when I export large data sets such as 5 million rows, the above code fails.
I want to see if there is a way to split the above results in batches of 100,000 rows and output multiple csv files through the browser.