【问题标题】:How to determine depth of root directory subdirectories PHP如何确定根目录子目录的深度 PHP
【发布时间】:2015-08-21 00:01:07
【问题描述】:

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

示例: 根文件夹:/

根目录:./subdirectory/

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

Level 0 将是根文件夹。

1 级是子目录。

Level 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' );

【问题讨论】:

    标签: php directory glob subdirectory


    【解决方案1】:

    你不能只数'/'的个数吗?

    例如

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

    【讨论】:

    • 计算/ 是我的想法之一。只是想找出正确的方法来做到这一点。我会测试你的代码然后回来。
    猜你喜欢
    • 2018-04-29
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 2020-01-15
    • 2014-04-03
    • 2014-12-18
    • 2023-03-26
    • 1970-01-01
    相关资源
    最近更新 更多