How to make CSV file items on separate lines?

I want to export pages with fields as CSV file. When it’s just a file on server, works well, but when it’s in Kirby template, lines in exported file are without breaks. Why is it happening and how do I add breaks? Example:

<?php

 header('Content-Type: text/csv');
 header('Content-Disposition: attachment; filename="sample.csv"');

$user_CSV[0] = array('first_name', 'last_name', 'age');
$user_CSV[1] = array('Quentin', 'Del Viento', '34');
$user_CSV[2] = array('Antoine', 'Del Torro', '55');
$user_CSV[3] = array('Arthur', 'Vincente', '15');

$fp = fopen('php://output', 'wb');
foreach ($user_CSV as $line) {

    fputcsv($fp, $line, ',');
}
fclose($fp);

?>

You are writing right to the browser, not into a file? Works fine for me when writing to file.

Not sure I understand. I am downloading it as a file, and still everything on one line.

I edited code. So I just go to that page and download is initiated.

Ok, with the edited code and nothing else in the template, that makes more sense. And works for me, btw.

Tested with Kirby 3.9.4, PHP 8.2

Strange, I had a bit different versions, but with Kirby 3.9.4, PHP 8.2 and still the same.

What’s your OS?

MacOS Ventura 13.2.1

try removing the final ?> to not have any trailing whitespace and add a die();

also from the fputcsv docs

Note : If PHP is not properly recognizing the line endings when reading files either on or created by a Macintosh computer, enabling the auto_detect_line_endings run-time configuration option may help resolve the problem.