tags:

views:

68

answers:

5

Hi,

I am creating text file in php, file has created in correct format from local pc and if i am trying online then no line break is there and not displaying formated.

I am using this code.

header("Content-type: application/txt");
header("Content-Length: ". sizeof($content));
header("Content-Disposition: inline; filename=". $file);
print $content; die;

If anyone have solutions please share with me.

Thanks in advance.

+1  A: 

Try

header('Content-Type: text/plain');
readfile($file);

This should be sufficient to send the file as plaintext. If this doesnt work for you for some obscure reason, a last resort would be to wrap the content in <pre> tags.

Gordon
+6  A: 

you have a typo:

header("Content-Length: ". sizeof($content));

also, you should use

header("Content-type: text/plain");
kgb
+1  A: 
  • The correct content type is text/plain
  • It's Content-Length, not Content-Lenght, and normally PHP will send this for you
  • Instead of die, better use exit.

The problem is probably because of the wrong MIME type - some browsers will ignore invalid content types and display it as HTML.

tdammers
What's wrong with using die? It's just an alias for exit.
Jeff Davis
'This language construct is equivalent to exit()' - from die man page... why use exit()?
kgb
I was under the impression that `die` signals an error, as in, you stop execution immediately because something went wrong, whereas `exit` means you stop execution immediately because you're done with whatever it is you want to do. Technically they're the same, but intuitively, I'd say they signal different intentions.
tdammers
Sorry spelling mistake here. It is correct in code "Content-Lenght".
Sanjay
A: 

I have already tried this "text/plain" but not display correct.

Sanjay
Post code. And look at the page source in your browser; if the newlines are displayed in the page source, but not the page, then it's not rendering as plain text. If there are no newlines in the page source either, then something else is going wrong, most likely $content for some reason doesn't contain any newlines.
tdammers
A: 
Shuja Shabandri