How to write file in UTF-8 format?

Starmaster picture Starmaster · Jan 29, 2011 · Viewed 156.3k times · Source

I have bunch of files that are not in UTF-8 encoding and I'm converting a site to UTF-8 encoding.

I'm using simple script for files that I want to save in utf-8, but the files are saved in old encoding:

header('Content-type: text/html; charset=utf-8');
mb_internal_encoding('UTF-8');
$fpath="folder";
$d=dir($fpath);
while (False !== ($a = $d->read()))
 {

 if ($a != '.' and $a != '..')
  {

  $npath=$fpath.'/'.$a;

  $data=file_get_contents($npath);

  file_put_contents('tempfolder/'.$a, $data);

  }

 }

How can I save files in utf-8 encoding?

Answer

user956584 picture user956584 · Jan 28, 2012

Add BOM: UTF-8

file_put_contents($myFile, "\xEF\xBB\xBF".  $content);