Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can export csv file , charset utf-8 instead utf-8 without BOM in php? [duplicate]

Possible Duplicate:
Encoding a string as UTF-8 with BOM in PHP

i can export csv file from php with this code

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

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

but this code create file with charset utf-8 without BOM but i need charset utf-8.
is way for this problem?

like image 918
aya Avatar asked Dec 09 '22 19:12

aya


1 Answers

Here's how I did it.

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();

Of course, you can substitute echo $csv_file_content with what you need.

like image 174
bear Avatar answered Dec 11 '22 07:12

bear