<?php
$file = fopen("testing.txt","w");
$txt = "Who let the dogs out?! 'r'n";
echo fwrite($file, $txt);
header("Cache-Control: private");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file.txt");
header("Content-Type: text/plain; ");
header("Content-Transfer-Encoding: binary");
readfile('testing.txt');
?>
第一个问题:我在PHP网站(http://php.net/manual/en/function.fopen.php)上读到,可能存在行结束的问题,但是当testing.txt
已经下载并打开时,以下行出现在前面添加了一些数字:
25Who let the dogs out?!
第二个问题:我注意到文件名是Resource id #3.txt
而不是testing.txt
。谷歌搜索显示,该错误消息通常与MySQL/PHP有关,但对我来说只是PHP。
我猜,这是因为你的代码中有这一行
echo fwrite($file, $txt);
试试这个代码
fwrite($file, $txt);
并更正
$filename = 'testing.txt';
header("Content-Disposition: attachment; filename=$filename.txt");