I am generating a CSV file with PHP and saving it on the hard disk (I do not want a download). The database charset is utf8_general_ci and there are chars in there like the degrees sign (°), which show up perfectly in PHPmyadmin. These chars need to be saved in the CSV file. Everything I do is in UTF-8, but the file gets saved with the encoding ISO-8859-1. If I start using the utf8_encode function, other chars show up (Â) when I convert it back to UTF-8 in excel or any other text editor. I do not know what is going wrong and I have spent hours to track it down, without success.
I have tried SET NAMES utf8 just to be sure it is UTF-8, but that just causes more strange characters and no UTF-8 encoding. Using MySQLi's set_charset does the same. With the header as Content-type:text/html;charset=UTF-8 the CSV still does not have utf-8 as default encoding, nor with any of these combinations.
Some code:
header("Content-type:text/html;charset=UTF-8");
$rMysqli = new mysqli("localhost", "user", "pass", "database");
$rCSV = fopen("test_".time().".csv", 'w');
$aCSVdata = array($var1, $var2, $var3);
fputcsv($rCSV, $aCSVdata, ",", "\"");
fclose($rCSV);
That is pretty much all is happening. Just normal CSV data, only with special chars such as ° and Ø.
utf8_encode. See What Every Programmer Absolutely, Positively Needs To Know About Encodings And Character Sets To Work With Text and Handling Unicode Front To Back In A Web App.SET NAMESin PHP. Usemysql_set_charset()(the C-API function) instead. MySQLi and ext/mysql both expose this directly (although we all know you shouldn't use ext/mysql, don't we boys and girls ;-) ), PDO exposes it through thecharsetDSN parametertext/htmlthrew me off for a minute, but you're not outputting the file so it's not wrong, it's just useless) - how are you viewing the file - in which program? If you are actually outputting the file (sending it to the client) you should usetext/csvas the content type. You could try writing a byte order mark to the beginning of the file, but this might create more problems than it solves.