PHP User
PHP User

Reputation: 2422

Codeigniter data to csv character encoding issue with Arabic

I'm using codeigniter as a framework with connection characterset

$config['charset'] = 'UTF-8';

Arabic is displayed correctly in the website and database. The issue is with exporting data to a csv file. The file is displayed correctly using code editor on cPanel but when I download the file and open it using excel I get this

issue with arabic

When I open it with notepad Arabic is displayed correctly but when I upload the file to facebook product catalog it doesn't display Arabic correctly too. here's the code

$handler = fopen('./directory/'.$fileName,'a+');

$exporteddata = 'availability,condition,description'.PHP_EOL;
 for ($x=0; $x<count($cat_products); $x++) {
      if(strlen(trim($cat_products[$x]->description)) == '0'){
         $description = ' ';
      }
      else{
         $description = $cat_products[$x]->description;
      }
      $exporteddata .= 'in stock,new,'.$description.PHP_EOL;
 }
fwrite($handler,$exporteddata);
fclose($handler);

Then redirect to a function that starts downloading the file using this code

public function get_file($file){
    header('Content-Encoding: UTF-8');
    header('Content-type: text/csv; charset=UTF-8');
    header("Content-Type: application/csv");
    header('Pragma: no-cache');
    header("Content-Disposition: attachment; filename=".basename($file) . "\"");
    echo "\xEF\xBB\xBF";

    $file = 'directory/'.$file;
    if (!is_file($file)) {
        header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
        echo 'File not found '.$file;
    } elseif (!is_readable($file)) {
        header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
        echo 'File not readable';
    } else {
        header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
        readfile($file);
    }
}

If we ignore the download function and just concentrate on data itself downloaded from file manager from cPanel I still have the issue so it's not just related the download function it's related to the writing function.

So how to fix the encoding issue so that Arabic is displayed correctly in the csv if opened with excel so it can be imported correctly using facebook.

[UPDATE] When I open the file in notepad and save it with ANSI encoding then open the new file with excel Arabic data is displayed correctly. Can I change the writing encoding to ANSI using php?

Upvotes: 2

Views: 2182

Answers (1)

PHP User
PHP User

Reputation: 2422

This solved the problem but still facebook getting wrong encoding I'll contact facebook for that. First

mb_convert_encoding($exporteddata, "Windows-1252", "UTF-8");
fwrite($handler,$exporteddata);
fclose($handler);

Second

public function get_file($file){
    $file = 'directory/'.$file;
    if (!is_file($file)) {
        header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
        echo 'File not found '.$file;
    } elseif (!is_readable($file)) {
        header($_SERVER['SERVER_PROTOCOL'] . ' 403 Forbidden');
        echo 'File not readable';
    } else {
        header($_SERVER['SERVER_PROTOCOL'] . ' 200 OK');
        header('Content-Encoding: UTF-8');
        header('Content-type: text/csv; charset=UTF-8');
        header("Content-Type: application/csv");
        header('Pragma: no-cache');
        echo "\xEF\xBB\xBF";
        header("Content-Disposition: attachment; filename=\"" . basename($file) . "\"");
        readfile($file);
    }
}

Adding echo "\xEF\xBB\xBF"; to the download function and mb_convert_encoding to the saving function solved theproblem and Arabic data is displayed correctly in both excel and notepad.

Upvotes: 1

Related Questions