Codeigniter数据以csv字符编码问题用阿拉伯语

我使用codeigniter作为连接字符集的框架

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

阿拉伯语在网站和数据库中正确显示。 问题在于将数据导出到csv文件。 该文件使用cPanel上的代码编辑器正确显示,但是当我下载文件,并使用Excel打开它,我得到这个

问题与阿拉伯语

当我用记事本打开它阿拉伯文显示正确,但是当我上传文件到Facebook产品目录它也不正确显示阿拉伯语。 这是代码

 $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); 

然后redirect到开始使用此代码下载文件的函数

 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); } } 

如果我们忽略下载function,只专注于从cPanel的文件pipe理器下载的数据本身,我仍然有这个问题,所以它不仅涉及下载function,它涉及到写作function。

所以如何解决编码问题,以便阿拉伯语在csv中正确显示,如果用excel打开,所以可以正确使用Facebook导入。

[更新]当我打开记事本中的文件,并保存与ANSI编码,然后用Excel打开新文件阿拉伯数据显示正确。 我可以改变写作编码为ANSI使用PHP?

这解决了这个问题,但仍然Facebook的错误编码我会联系Facebook的。 第一

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

第二

 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); } } 

添加回声“\ xEF \ xBB \ xBF”; 下载function和mb_convert_encoding保存function解决了问题,阿拉伯数据在excel和记事本都正确显示。