title: Codeigniter data to csv character encoding issue with Arabic
body: <p>I'm using codeigniter as a framework with connection characterset</p>
  
  <pre><code>$config['charset'] = 'UTF-8';
  </code></pre>
  
  <p>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</p>
  
  <p><a href="https://i.stack.imgur.com/QorT6.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QorT6.png" alt="issue with arabic"></a></p>
  
  <p>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</p>
  
  <pre><code>$handler = fopen('./directory/'.$fileName,'a+');
  
  $exporteddata = 'availability,condition,description'.PHP_EOL;
   for ($x=0; $x&lt;count($cat_products); $x++) {
        if(strlen(trim($cat_products[$x]-&gt;description)) == '0'){
           $description = ' ';
        }
        else{
           $description = $cat_products[$x]-&gt;description;
        }
        $exporteddata .= 'in stock,new,'.$description.PHP_EOL;
   }
  fwrite($handler,$exporteddata);
  fclose($handler);
  </code></pre>
  
  <p>Then redirect to a function that starts downloading the file using this code</p>
  
  <pre><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);
      }
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>[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?</p>