Convert utf-8 to GBK
$data = iconv("utf-8","GBK//IGNORE",$data);Convert GBK to utf-8
$data = iconv("GBK","utf-8//IGNORE",$data);IGNORE, let the iconv() function ignore the error and continue execution.
Simple use in file_put_contents and file_get_contents
<?php
$str = '少时诵诗书所所';
$filename = '文件名.txt';
file_put_contents($filename, $str);
$st = file_get_contents($filename);
echo $st;The following error will be reported
Warning: file_put_contents(文件名.txt): failed to open stream
Warning: file_get_contents(文件名.txt): failed to open streamThe file name is Chinese garbled. Convert the encoding of non-GBK character set to GBK.
Generally, the file is encoded in utf-8, but the system defaults to gbk. So first convert the file name to gbk and then read it.
<?php
$str = '少时诵诗书所所';
$filename = '文件名.txt';
file_put_contents(@iconv('UTF-8','GBK',$filename),$str);
$st = file_get_contents(@iconv('UTF-8','GBK',$filename));
echo $st;Normal execution---convert Chinese file name encoding to GBK without garbled characters
Of course, the content can also be forced to be converted to utf-8 to prevent garbled characters.