Home  >  Article  >  Backend Development  >  php遍历中文目录出现乱码

php遍历中文目录出现乱码

WBOY
WBOYOriginal
2016-06-06 20:07:411683browse

遍历windows下的某个目录,将文件目录或文件显示出来。

<code><?php ini_set('display_errors', 1);
$dir_handle=opendir("D:\\UPUPW_AP5.5\\temp\\");
//var_dump(mb_detect_order());
while(($readdir=readdir($dir_handle))!=false)
{
    $newchar=mb_convert_encoding($readdir,"gb2312","UTF-8");
    $char=iconv("UTF-8","gb2312",$readdir);
    echo $readdir."<br/>";

}
closedir($dir_handle);
?></code>

用了mb_convert_encoding和iconv函数编码转换都不行,php文件编码是UTF-8。

回复内容:

遍历windows下的某个目录,将文件目录或文件显示出来。

<code><?php ini_set('display_errors', 1);
$dir_handle=opendir("D:\\UPUPW_AP5.5\\temp\\");
//var_dump(mb_detect_order());
while(($readdir=readdir($dir_handle))!=false)
{
    $newchar=mb_convert_encoding($readdir,"gb2312","UTF-8");
    $char=iconv("UTF-8","gb2312",$readdir);
    echo $readdir."<br/>";

}
closedir($dir_handle);
?></code>

用了mb_convert_encoding和iconv函数编码转换都不行,php文件编码是UTF-8。

这两个函数,转过来转过去,不是原来那个编码吗

<code class="php">
function listDir($dir){
    $arr = [];
    foreach (glob($dir.'*') as $v) {
        $arr[] = mb_convert_encoding($v, 'utf-8', 'gbk'); 
        if(is_dir($v)){
            $arr = array_merge($arr, listDir($v));
        }
    }
    return $arr;
}
print_r(listDir('D:\\UPUPW_AP5.5\\temp\\'));</code>
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