<?php $filename="filename.csv"; header('Content-Type: application/csv; charset=UTF-8'); header("Content-Disposition: attachment; filename=$filename"); echo "\xEF\xBB\xBF"; echo "id,name,tel,addr\n"; echo "001,張三,0933123123,中文地址資料\n"; echo "002,李四,0933222333,中文地址資料\n";
\xEF\xBB\xBF 就是 BOM 字元 (Byte Order Mark)
csv 檔案最前面寫入 BOM 字元,這樣 excel 匯入 csv 檔案時,中文字就不會有亂碼