Search code examples
phpcsvline-breaks

\n not working when creating dynamic .csv file for user download in PHP


I am trying to create a dynamically generated csv file for user download from a piece of html within my application. Everything works fine except for the \n doesn't seem to be registering in the file that is output. It is showing up as nothing in the generated file. The lines are not separated.

I have tried entering the echo output manually and still \n does not produce a line break in my output. I'm wondering if it has something to do with my headers as I am also getting a console notice:

Resource interpreted as Document but transferred with MIME type text/csv:

<?PHP
$tasks = $_GET['text'];

header("Content-type: text/csv");
header("Content-Disposition: attachment; filename='myProject.csv'");
header("Pragma: no-cache");
header("Expires: 0");

$tasks = str_replace('</br>',"\n",$tasks);

echo $tasks;

exit();
?>

Solution

  • $tasks = str_replace('</br>',"\n",$tasks);
    

    You seem to be mixing single quotes and double quotes. Is that ok in php? (normally single quotes are used for char, like \n here).

    Also: you might try "\r\n".