在PHP中创建txt文件

时间:2010-08-03 13:45:21

标签: php

我在php中创建文本文件,文件已从本地pc以正确的格式创建,如果我在线尝试,那么没有换行符,也没有显示格式化。

我正在使用此代码。

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

如果有人有解决方案,请与我分享。

提前致谢。

4 个答案:

答案 0 :(得分:6)

你有一个错字:

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

另外,你应该使用

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

答案 1 :(得分:1)

尝试

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

这应该足以将文件作为纯文本发送。如果由于某些不明原因这对你不起作用,最后的办法是将内容包装在<pre>标签中。

答案 2 :(得分:1)

  • 正确的内容类型为text/plain
  • 它是Content-Length,而不是Content-Lenght,通常PHP会为您发送
  • 而不是die,更好地使用exit

问题可能是因为MIME类型错误 - 某些浏览器会忽略无效的内容类型并将其显示为HTML。

答案 3 :(得分:1)

试试这个

$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;