2015-06-05 41 views
0

我正在使用glob递归来查找其中包含index.php文件的所有目录。我想要做的是弄清楚深度是否超出了某个水平。如何确定根目录子目录的深度PHP

实施例: 根文件夹:/

根的子目录:​​

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

级别0将是根文件夹。

等级1将是子目录。

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

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

伪代码示例:

if ($subdirectory_depth >= 2) { 
    //do something; 
} 

这里是我的水珠脚本

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

回答

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

计算'/'是我有的想法之一。试图找出正确的方法来做到这一点。我会测试你的代码并回来。 – Mike