I have the below code, where I generate random numbers and try to download them as a CSV file.
If I try the CSV code snippet alone on a different file it works, but in this code, it just does not work. I do see the random numbers echoed, but it does not download the CSV file.
<?php
$dataarray=array();
function convert_to_csv($input_array, $output_file_name, $delimiter)
{
$temp_memory = fopen('php://memory', 'w');
foreach ($input_array as $line)
{
fputcsv($temp_memory, $line, $delimiter);
}
fseek($temp_memory, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
fpassthru($temp_memory);
}
for ($i = 0; $i <= 25000; $i++)
{
$num = (rand(50,110));
array_push($dataarray,"$num");
echo "Hearbeat #" .$i . "\t\t ------ " . $num;
echo "<br>";
}
convert_to_csv($dataarray, 'data_as_csv.csv', ',');
?>