TanGio
TanGio

Reputation: 766

Encoding characters when exporting using php in csv format

I have a problem with my export.

So I tried to export data to .csv with the following code :

        $output = fopen('php://output', 'w');
        $sFileName = 'test.csv';
        header('Content-Disposition: attachement; filename="' . $sFileName . '";');
        header("Content-Type:application/csv;charset=UTF-8");
        fwrite($output, "sep=;\n");
        fputcsv($output, array('Nom', 'Prenom'), ";");
        foreach ($aFilterGifts as $value) {
            fputcsv($output, $value, ";");
        }
        fpassthru($output);
        fclose($output);
        exit;

Where the $aFilterGifts is an array with data. But If I have in Nom, data like this Fossé, when I export I get FossГ©.

Is there a solution? Thank you in advance.

Upvotes: 0

Views: 610

Answers (1)

TanGio
TanGio

Reputation: 766

I found the error, it's necessairy to make $output = fopen('php://output', 'w'); after all headers,so thhe code is like this :

    $sFileName = 'test.csv';
    header('Content-Disposition: attachement; filename="' . $sFileName . '";');
    header("Content-Type:application/csv;charset=UTF-8");
    $output = fopen('php://output', 'w');

Upvotes: 1

Related Questions