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

I'm doing:

file_put_contents("txt/myfile.txt", $fileContents);

I have tried many ways to force my text file to be ANSI, like:

$fileContents = mb_convert_encoding($fileContents , mb_detect_encoding($fileContents , mb_detect_order(), true), 'WINDOWS-1252');

I have also tried:

$fileContents = iconv("ISO-8859-1", "WINDOWS-1252", $fileContents );

I need ANSI because the text file should look nice when I open it with the "type" command from MS-DOS (cmd.exe in Windows 7)

If I open my current file I can see the UTF-8 BOM:

C:\Users\XXX>type C:\myfile.txt

´╗┐V017666999 00000000000000005350005122013

If I open the file with Notepad++ and apply "Convert to ANSI" I get (what I need):

C:\Users\XXX>type C:\myfile.txt

V017666999 00000000000000005350005122013

Is there any way I can fix this? Thanks in advance.

解决方案

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']);

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值