-1

I am facing an issue while exporting CSV.

headers look like this:

header('Content-Encoding: unicode');
header('Content-type: text/csv; charset=unicode');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename="'.$fname.'.csv"');
header("Content-length: 21474836470"); // tells file size set it to maximum    
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); //Must revalidate
header('Pragma: no-cache');
header('Expires: 0');

Data is printed correctly but it messed up in CSV I have tried changing Content-Encoding to utf-8, iso-8859-1 and some more.

cello
  • 5,356
  • 3
  • 23
  • 28
  • 2
    You're going to have to give an example of the way the data is *"messed up"*. It should be noted, however, that your `Content-Length` header should match the actual length of the response. – Phylogenesis Mar 26 '15 at 08:30
  • let me clarify. I show word "tester_f_name Herm`óinia" as "tester_f_name Herm`óinia " – yousaf qamar Mar 26 '15 at 14:19
  • Then your file is encoded as UTF-8. Excel appears to be decoding it as ISO-8859-1. Have you tried to put a [BOM](http://en.wikipedia.org/wiki/Byte_order_mark) at the start of the file? In my testing this appears to allow Excel to read UTF-8 encoded files correctly. – Phylogenesis Mar 26 '15 at 14:25
  • And by that I mean, run `echo "\xef\xbb\xbf";` before you write out the CSV file and after you've written the headers. – Phylogenesis Mar 26 '15 at 14:37
  • There is same issue with email, as I am using codeignitor library – yousaf qamar Mar 26 '15 at 15:36

1 Answers1

4
  1. Content-Encoding does not denote the character encoding, it denotes a transport encoding. For example it'll says gzip if the HTTP response is compressed.
  2. unicode is not an encoding, it's a defining standard. UTF-8, ISO-5589-1 etc. are concrete encodings.
  3. It largely depends on which application you're opening the resulting file with. Should that be Excel I'll let you know that it notoriously sucks with encodings. Search for the many questions on SO about Excel and CSV encodings, for instance How can I output a UTF-8 CSV in PHP that Excel will read properly?. Otherwise, we'll need more information to be able to say anything.
Community
  • 1
  • 1
deceze
  • 510,633
  • 85
  • 743
  • 889
  • thanks, I am using Excel 2013 for it on win 7. Plz let me know how can I fix it and make that csv compatible for all type of applications – yousaf qamar Mar 26 '15 at 14:18