且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP-如何使用ANSI编码保存文本文件?

更新时间:2023-02-20 11:00:33

现在,我知道发生了什么,该文件正在正确创建,但是在我下载该文件时会添加不需要的BOM.

Now I know what happened, the file is being created correctly, but the undesired BOM is added when I download it.

这是问题,我只需要更改一下:

This is the problem, I just had to change this:

/* Bad code */
header('Content-disposition: attachment; filename='.$_GET['filename']);
header('Content-type: application/txt');
readfile($_GET['filename']);

为此(下载为二进制文件,使其保持完整):

to this (Download as binary file so it remains intact):

/* Good code */
header('Content-disposition: attachment; filename='.$_GET['filename']);
header('Content-type: application/txt');
header('Content-Transfer-Encoding: binary');
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
ob_clean();
flush();
readfile('txt/'.$_GET['filename']);

(这最初是作为对问题的修改而发布的,但@Daniel建议发布答案以进行澄清).

(This was originally posted as an edit on the question, but @Daniel suggested posting an answer for clarification).