PHP Streaming CSV Always Adds UTF-8 Specification

The following code gets the "report string" as an array and uses fputcsv to convert it to CSV. Everything works fine, except for the fact that, regardless of the encoding used, it puts UTF-8 at the top of the file. This is extremely annoying because A) I specify iso and B). We have many users who use tools that display UTF-8 bom as garbage characters.

I even tried writing the results to a string, splitting the UTF-8 specification, and then I repeat it and still get it. Is it possible that the problem is related to Apache? If I changed fopen to a local file, it writes it fine without the UTF-8 specification.

header("Content-type: text/csv; charset=iso-8859-1");
header("Cache-Control: no-store, no-cache");
header("Content-Disposition: attachment; filename=\"report.csv\"");

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

for($i = 0; $i < $report->rowCount; $i++) {
    fputcsv($outstream, $report->getTaxMatrixLineValues($i), ',', '"');
}
fclose($outstream);

exit;
+2
2

, php , php.

+7

, , print implode, ?

header("Content-type: text/csv; charset=iso-8859-1");
header("Cache-Control: no-store, no-cache");
header("Content-Disposition: attachment; filename=\"report.csv\"");

for($i = 0; $i < $report->rowCount; $i++) {
    print(implode(',',$report->getTaxMatrixLineValues($i)));
}

, .

0

Source: https://habr.com/ru/post/1622871/


All Articles