Level 80
@Lina You could maybe try using the PHP League CSV package, which makes reading and writing CSV files easier: https://csv.thephpleague.com/
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
$headers = array(
"Content-type" => "text/csv; charset=UTF-8",
"Content-Disposition" => "attachment; filename=file.csv",
"Pragma" => "no-cache",
"Expires" => "0"
);
$callback = function() use ($data) {
$file = fopen('php://output', 'w');
// headers are not writting on the first line.
fputcsv($file, array('Column 1', 'Column 2', 'Column 3', 'Column 4', 'Column 5'));
// Sample data. This can be fetched from mysql too
$data = array(
array('Data 11', 'Data 12', 'Data 13', 'Data 14', 'Data 15'),
array('Data 21', 'Data 22', 'Data 23', 'Data 24', 'Data 25'),
array('Data 31', 'Data 32', 'Data 33', 'Data 34', 'Data 35'),
array('Data 41', 'Data 42', 'Data 43', 'Data 44', 'Data 45'),
array('Data 51', 'Data 52', 'Data 53', 'Data 54', 'Data 55')
);
// save each row of the data
foreach ($data as $row)
{
fputcsv($file, $row);
}
// Close the file
fclose($file);
};
return Response::stream($callback, 200, $headers);
Result: http://prntscr.com/hwfsfb
Please or to participate in this conversation.