递归显示文件夹下的所有目录(带绝对路径):并且解决中文乱码问题

来源:互联网 发布:java与xml 编辑:程序博客网 时间:2024/05/22 05:01

递归显示文件夹下的所有目录(带绝对路径):并且解决中文乱码问题

源码git地址:https://github.com/a542970465/tips.git

代码如下:


<?php

###################
# User:Lei                    #
# Time:2017/08/30      #
###################




// Recursive shows all directory in the folder (with absolute path):to solve the Chinese messy code


function show_all_dir($dir)
{


$dir = iconv('utf-8', 'gb2312', $dir);


$result = array();
$handle = opendir($dir);


if ($handle) {


while(($file = readdir($handle)) != false) {


if ($file != '.' && $file != '..') {


$cur_path = $dir . DIRECTORY_SEPARATOR . $file;


if (is_dir($cur_path)) {


// to convert into Chinese
$cur_path = iconv('gb2312', 'utf-8', $cur_path);
$result['dir'][$cur_path] = show_all_dir($cur_path);
} else {
$cur_path = iconv('gb2312', 'utf-8', $cur_path);
$result['file'][] = $cur_path;
}
}
}
closedir($handle);
}
return $result;
}


$dir = 'E:\测试';
echo '<pre>';
var_dump(show_all_dir($dir));
阅读全文
0 0
原创粉丝点击