O
O
Oleg As2017-12-15 19:56:30
PHP
Oleg As, 2017-12-15 19:56:30

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 will give me back the following
$ enca -g file.csv
Universal transformation format 8 bits; UTF-8
Doubly-encoded to UTF-8 from CP1251

But if you add the following processing:
foreach($data as $k => $rec){
                foreach($rec as $key => $value){
                  $data[$k][$key] = mb_convert_encoding($value, "Windows-1251", "UTF-8");
                }
            }

and again export the file to csv enca will return the following response:
7bit ASCII characters
or
KOI8-R Cyrillic
Surrounded by/intermixed with non-text data
The file must accept 1C, which it actually cannot do.
enca should return MS-Windows code page 1251
Tried to modify .htaccess to change encoding.
The file is sent like this:
header('Content-type: text/csv;');
            header("Content-Disposition: attachment;filename=file.csv'");
            header('Cache-Control: must-revalidate');
            header('Content-Length: ' . filesize($file));
        }
        readfile($file);

I tried to set them up - the file is still given not as it should be.
What else can be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2017-12-15
@eXcNightRider

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 question

Ask a Question

731 491 924 answers to any question