\n not creating a new line in downloadable text file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
robing
Forum Newbie
Posts: 2
Joined: Mon Jun 01, 2009 3:06 pm

\n not creating a new line in downloadable text file

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

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

Post 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.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

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

Post by mikemike »

For ease of testing WordPad opens files 'correctly' (ie it reads \n as it's intended)
robing
Forum Newbie
Posts: 2
Joined: Mon Jun 01, 2009 3:06 pm

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

Post by robing »

That has sorted my problem. Thanks for all your help. Much appreciated.
Post Reply