Home  >  Article  >  Backend Development  >  php读取中文目录乱码的解决办法

php读取中文目录乱码的解决办法

WBOY
WBOYOriginal
2016-06-20 13:00:261788browse

前不久有个项目涉及到使用 php 读取文件目录,读取文件目录这个本身没什么问题,但读取的时候遇到中文目录竟然发生乱码了,一时不知道怎么办了,很久没有碰到这种问题了,研究了一番,得出几种解决办法,现列举一下(将使用 php 读取文件目录的方法也一并附上):

<p><?php</p>//要读取的目录<br />$folder="D:/www";<br />//打开目录<br />$fp=opendir($folder);<br />//阅读目录<br />while(false!=$file=readdir($fp)){<br />	//列出所有文件并去掉'.'和'..'<br />	if($file!='.' &&$file!='..'){<br />	//$file="$folder/$file";<br />		$file="$file";<br />		//赋值给数组<br />		$arr_file[]=$file;<br />	}<br />}<br />//输出结果<br />if(is_array($arr_file)){<br />	while(list($key,$value)=each($arr_file)){<br />		echo "$key=>$value<br>";<br />	}<br />}<br />//关闭目录<br />closedir($fp);<br /><p>?></p>

(1)在输出前做一个转换,加上如下代码:

$value = iconv("UTF-8","gb2312",$value); //或者 iconv("gb2312","UTF-8",$value);

(2)所有文件编码都统一为 UTF-8 或者 gb2312


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn