[英]PHP CSV export saves as unicode text instead of CSV
I have a problem with exporting to csv from php. 我从php导出到csv时遇到问题。 When I export to csv it works fine.
当我导出到CSV时,效果很好。 I can open it and edit it.
我可以打开它并对其进行编辑。 But when I want 'to save as' the edited file it says save as Unicode Text (.txt)
但是,当我要“另存为”编辑后的文件时,它说另存为Unicode文本(.txt)
I would like to Save directly as CSV. 我想直接另存为CSV。 The exported file name has a CSV extension.
导出的文件名具有CSV扩展名。 This is my code:
这是我的代码:
$filename = "export-".date("d-m-Y").".csv";
ob_end_clean();
$filePath = 'php://output';
header('Content-Encoding: UTF-8');
header('Content-Type: application/csv; charset=UTF-8');
header('Content-Disposition: attachement; filename="'.$filename.'";');
$f = fopen($filePath, 'w');
echo "\xEF\xBB\xBF"; // UTF-8 BOM
$header = array();
// GET CURRENT HEADERS
foreach ($data[0] as $key => $value) {
array_push($header,$key);
}
fputcsv($f, $header, $delimiter);
//unset($data['headers']);
foreach ($data as $line) {
fputcsv($f, $line, $delimiter);
}
fclose($f);
I tried several different headers, but none worked. 我尝试了几种不同的标题,但没有一个起作用。 I Googled and searched Stackoverflow but I seem to be the only one having this problem.
我用Google搜索并搜索了Stackoverflow,但我似乎是唯一出现此问题的人。 Is someone familiar with this?
有人熟悉吗?
Have a look at the UTF-8 BOM. 看看UTF-8 BOM。
Had a similar issue, removing the BOM prevented this behaviour, but need to confirm that UTF-8 characters are still displayed correctly etc. Not fully tested yet. 发生了类似的问题,删除BOM阻止了此行为,但是需要确认UTF-8字符仍然可以正确显示等。尚未经过全面测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.