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

aya*_*aya 4 php csv utf-8 character-encoding

可能重复:
在PHP中将字符串编码为带有BOM的UTF-8

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

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

$output = fopen('php://output', 'w');
Run Code Online (Sandbox Code Playgroud)

但是这段代码用charset创建文件,utf-8 without BOM但我需要charset utf-8.
是这个问题的方法?

bea*_*ear 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();
Run Code Online (Sandbox Code Playgroud)

当然,你可以echo $csv_file_content用你需要的东西代替.