首页 > 开发 > PHP > 正文

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

2024-05-04 22:25:01
字体:
来源:转载
供稿:网友
代码如下:
<?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;
    }
?>
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表