我使用 php 创建了一个可以在按下链接时下载的文件。
已经尝试了多种代码变体。没有理由这个不应该工作:
$output = 'test spaces, test
enter, test n, \ntest nr, \n\rtest nt, \n\ttest html-br, <br /><br>end of line';
$output .= '\r\n';
$output .= 'blahblah';
header('Content-type: text/plain');
header('Content-Disposition: attachment;filename="'.$filename.'.txt"');
print $output;
exit;
但是返回的文件以我没有插入的两个空格开头。\r\n 上没有换行符。
如果我在 notepad++ 中打开文件,我会在“enter”上得到一个换行符 在普通的记事本中,中断甚至不存在。下载的文本文件中的输出在引号之间是这样的:
In notepad++
" test spaces, test
enter, test n, \ntest nr, \n\rtest nt, \n\ttest html-br, <br /><br>end of line\r\nblahblah"
所以。我究竟做错了什么?在 header 之前输出一些东西,所以 header 不起作用?
更新:感谢您的帮助。两个正确答案同时出现。订购“最老的第一”时的第一个答案被标记为正确答案。使用单引号解决了这个问题。
输出开头的两个空格问题与给定示例中的 code/php 无关。它是所用框架的产物。
最佳答案
换行符(使用 \n
和 \r
)只能在双引号中识别,不能在单引号中识别。
关于php 创建文本文件。换行符是错误的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7202570/