Php 如何确定根目录子目录的深度

Php 如何确定根目录子目录的深度,php,directory,glob,subdirectory,Php,Directory,Glob,Subdirectory,我使用glob递归查找所有包含index.php文件的目录。我想做的是弄清楚深度是否超过了某个水平 例如: 根文件夹:/ 根目录的子目录:./子目录/ 子目录的子目录:./dubdirectory/subdirectory/ 级别0将是根文件夹 级别1将是子目录 级别2将是子目录的子目录 如何确定深度是否达到2级或更高 伪代码示例: if ($subdirectory_depth >= 2) { //do something; } 这是我的glob脚本 if ( ! functi

我使用glob递归查找所有包含index.php文件的目录。我想做的是弄清楚深度是否超过了某个水平

例如: 根文件夹:/

根目录的子目录:./子目录/

子目录的子目录:./dubdirectory/subdirectory/

级别0将是根文件夹

级别1将是子目录

级别2将是子目录的子目录

如何确定深度是否达到2级或更高

伪代码示例:

if ($subdirectory_depth >= 2) {
    //do something;
}
这是我的glob脚本

if ( ! function_exists('glob_recursive'))
{
    function glob_recursive($pattern, $flags = 0 )
    {
        $files = glob($pattern, $flags);
        foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR) as $dir)
        {
            if (preg_match('#/(?:cgi-bin|css|images|includes|test)#', $dir)) continue; // exclude these directories

            $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
        }
        return $files;
    }
}

$dirlist = glob_recursive( $dir_structure.'*index.php' );

你不能数一数“/”吗

例如

数数数字是我的想法之一。只是想找出正确的方法。我会测试你的代码然后回来。
<?
if ( ! function_exists('glob_recursive'))
{
    function glob_recursive($pattern, $flags = 0 )
    {
        $files = glob($pattern, $flags);
        foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR) as $dir)
        {
            if (preg_match('#/(?:cgi-bin|css|images|includes|test)#', $dir)) continue; // exclude these directories

            $files = array_merge($files, glob_recursive($dir.'/'.basename($pattern), $flags));
        }
        return $files;
    }
}

$dirlist = glob_recursive( '/home/storage/e/63/25/phpsandbox/ToBeDeleted/2015/may/'.'*index.php' );
$maxdept = 10;
foreach($dirlist as $filepath) {
    $dept = substr_count($filepath, '/');
    echo '<br>'.$filepath.' Dept: '.$dept;
    if($dept > $maxdept) {
        echo ' (Deeper than '.$maxdept.')';
    }
}
?>