Page 1 of 1

\n not creating a new line in downloadable text file

Posted: Mon Jun 01, 2009 3:21 pm
by robing
I am trying to present a text file for the user to download. This text file is dynamicaly created using php based on user input. It is supposed to span several lines but it is just sticking everything on one line. Here is the code:

Code: Select all

 
<?php
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename="hello.txt"');
echo "Hello\nHow are you?\nIt is nice weather" . $_POST['text'];
?>
 
What I want to get is a text file like this:

Hello
How are you?
It is nice weather(USER INPUT)

But I am getting this:

HelloHow are you?It is nice weather(USER INPUT)

If I get rid of the

Code: Select all

header('Content-Disposition: attachment; filename="hello.txt"');
then it works but it is just displayed in the browser and I want a downloadable file. Does anyone know why this is happening.
Appreciate any help. Thanks.

Re: \n not creating a new line in downloadable text file

Posted: Mon Jun 01, 2009 5:21 pm
by califdon
It's putting a newline character in the file, but what you are viewing the file with determines how it will render it. Typically, for example, a Windows text editor (Notepad) wants to see 2 characters, a return followed by a newline:
"One line\r\nAnotherline". Linux systems, as I recall, render a newline, and I think Macs do, too. It's an age-old problem.

Re: \n not creating a new line in downloadable text file

Posted: Mon Jun 01, 2009 5:55 pm
by mikemike
For ease of testing WordPad opens files 'correctly' (ie it reads \n as it's intended)

Re: \n not creating a new line in downloadable text file

Posted: Tue Jun 02, 2009 5:45 am
by robing
That has sorted my problem. Thanks for all your help. Much appreciated.