php嵌套数据

<?php

header('Content-Type: text/html; charset=utf-8');
$path = './';
$list = readDirsNested($path);
echo '<pre>';
print_r($list);

/**
 * @param 目录地址
 */
function readDirsNested($path) {
    $nested = array();//存储当前目录下所有的文件
    $dir_handle = openDir($path);
    while(false !== $file=readDir($dir_handle)) {
        if ($file=='.' || $file=='..') continue;

        //创建当前文件信息
        $fileinfo = array();
        // $fileinfo['name'] = $file;
        $fileinfo['name'] = iconv('gbk', 'utf-8', $file);

        //判断当前是否为目录
        if(is_dir($path . '/' . $file)) {
            //是目录
            $fileinfo['type'] = 'dir';
            $func_name = __FUNCTION__;
            $fileinfo['nested'] = $func_name($path . '/' . $file);
        } else {
            //是文件
            $fileinfo['type'] = 'file';
        }
        //存入$nested数组内
        $nested[] = $fileinfo;
    }

    closeDir($dir_handle);
    return $nested;
}

 

posted on 2017-07-16 16:46  huodaihao  阅读(137)  评论(0编辑  收藏  举报

导航