开发者

Create txt file in PHP

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 ha开发者_开发百科ve solutions please share with me.

Thanks in advance.


you have a typo:

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

also, you should use

header("Content-type: text/plain");


  • 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.


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.


Try this

$content = str_replace("\n\r","\n",$content);

$content = str_replace("\r","\n",$content);

header("Content-type: text/plain");

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

header("Content-Disposition: inline; filename=". $file);

print $content; die;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜