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

时间:2015-06-05 22:19:47

标签: php directory glob subdirectory

我使用glob递归来查找包含index.php文件的所有目录。我想要做的是弄清楚深度是否超出一定水平。

实施例: 根文件夹:/

root的子目录:./subdirectory/

子目录的子目录:./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' );

1 个答案:

答案 0 :(得分:0)

你不能只计算'/'的数量吗?

E.g。

<?
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.')';
    }
}
?>