Answer the question
In order to leave comments, you need to log in
How can I solve the problem with the encoding of a CSV file created with fputcsv?
Initial conditions:
1. Database in utf8_general_ci
2. Some query is executed, an array is returned.
If without processing, upload the data to csv with the following commands:
$fp = fopen('/var/project/documents/file.csv', 'wb');
fputcsv($fp, $title, ';', '"');
foreach($data as $res){
fputcsv($fp, $res, ';', ' ');
}
fclose($fp);
$ enca -g file.csv
Universal transformation format 8 bits; UTF-8
Doubly-encoded to UTF-8 from CP1251
foreach($data as $k => $rec){
foreach($rec as $key => $value){
$data[$k][$key] = mb_convert_encoding($value, "Windows-1251", "UTF-8");
}
}
header('Content-type: text/csv;');
header("Content-Disposition: attachment;filename=file.csv'");
header('Cache-Control: must-revalidate');
header('Content-Length: ' . filesize($file));
}
readfile($file);
Answer the question
In order to leave comments, you need to log in
Until the very end, work with utf-8, and then do mb_convert_encoding to the finished file
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question