1.首先处理好写入txt文件的内容,特别注意编码问题,可以用mb_convert_encoding函数处理编码问题;
2.引入头文件;
3.输出内容,可以直接下载到本地。
$content = "文本内容";//文件内容
header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Content-Disposition: attachment; filename = test.txt"); //文件命名
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
echo $content;