1

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 Ø.

18
  • Check if your PHP script is in UTF-8. Also, how do you know that the file is in ISO-8859-1? Commented Dec 17, 2012 at 8:55
  • 2
    Opening it in some program and that program detecting the file as ISO-8859 doesn't mean anything. It just means the program didn't know any better and chose ISO, it doesn't mean the file is necessarily encoded in that format. And please don't use 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. Commented Dec 17, 2012 at 9:10
  • 3
    @Rune Never use SET NAMES in PHP. Use mysql_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 the charset DSN parameter Commented Dec 17, 2012 at 10:08
  • 1
    @Rune OK, well there's nothing wrong there (the text/html threw 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 use text/csv as 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. Commented Dec 17, 2012 at 10:18
  • 1
    The Office suite is the worst when it comes to encodings. They notoriously suck at it. Try including a BOM, MS products really like it. Commented Dec 17, 2012 at 10:30

1 Answer 1

7

...long story of the comments short...

Microsoft Office is the worst when it comes to encodings. It notoriously sucks at it.

(I.e. everything was fine except Excel.)

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.