如何在PHP中导出csv文件,charset utf-8而不是没有BOM的utf-8?

时间:2012-12-08 05:55:03

标签: php csv utf-8 character-encoding

  

可能重复:
  Encoding a string as UTF-8 with BOM in PHP

我可以使用此代码从php导出csv文件

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');

$output = fopen('php://output', 'w');

但此代码使用charset utf-8 without BOM创建文件,但我需要charset utf-8
是解决这个问题的方法吗?

1 个答案:

答案 0 :(得分:14)

我是这样做的。

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream'); 
header('Content-Disposition: attachment; filename=file.csv'));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
echo $csv_file_content;
exit();

当然,您可以将echo $csv_file_content替换为您需要的内容。