当前位置:Gxlcms > PHP教程 > PHP遍历某个目录下的所有文件和子文件夹的实现代码

PHP遍历某个目录下的所有文件和子文件夹的实现代码

时间:2021-07-01 10:21:17 帮助过:35人阅读

代码如下:
<?php
 function read_all_dir ( $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 ) )
                    {
                        $result['dir'][$cur_path] = read_all_dir ( $cur_path );
                    }
                    else
                    {
                        $result['file'][] = $cur_path;
                    }
                }
            }
            closedir($handle);
        }
        return $result;
    }
?>

人气教程排行