简体   繁体   English

按名称和返回路径递归搜索目录

[英]Recursively search Directory by name and return path

I have a directory structure with up to 10 levels (may vary), given the name of the directory, I would like to return the complete path of the first directory that matches the name of the given directory. 我有一个目录结构,最多10个级别(可能会有所不同),给定目录的名称,我想返回第一个目录的完整路径,该路径与给定目录的名称相匹配。

public function LocalDirSearch($clouse) {
        $path    = $clouse[0];
        $search = $clouse[1];
        $result = $clouse[2];
        $dirs   = glob($path, GLOB_ONLYDIR);
        foreach ($dirs as $dir) {
            if(basename($dir)==$search){
                return $dir;
                break;
            }
            $this->LocalDirSearch([$dir,$search,$result]);
        }
    }

I made this script but it does not work properly I do not know what exactly it is missing and it runs indefinitely ... no error output... 我制作了这个脚本,但是它无法正常工作我不知道它究竟缺少了什么,它无限期地运行......没有错误输出......

Update 更新

this the directory structure: 这个目录结构:

+--pdf
+---+---i-pdf-0034
+---+---+---form
+---+---+---+---data
+---+---+---+---other
+---+---i-pdf-0045
+---+---+---form
+---+---+---+---data
+---+---+---+---other
+---+---i-pdf-0056
+---+---+---form
+---+---+---+---data
+---+---+---+---other
+--doc
+---+---i-doc-0034
+---+---+---form
+---+---+---+---data
+---+---+---+---other
+---+---i-doc-0045
+---+---+---form
+---+---+---+---data
+---+---+---+---other
+---+---i-doc-0056
+---+---+---form
+---+---+---+---data
+---+---+---+---other

function call: 功能调用:

$this->LocalDirSearch(['C:/xampp/htdocs/files','i-pdf-0045']);

Script to search directory: 搜索目录的脚本:

public function LocalDirSearch($clouse) {
    $path   = $clouse[0];
    $search = $clouse[1];
    $dirs = glob($path . '/*', GLOB_ONLYDIR);
    foreach ($dirs as $dir) {
        if (basename($dir) == $search) {
            return $dir;
        }else{
            // return the result of the recursive call
            return $this->LocalDirSearch([$dir, $search]);  
        }
    }
}

Ouput of recursive print nav in directory: 目录中的递归打印导航输出:

'C:/xampp/htdocs/files/'
'C:/xampp/htdocs/files/pdf/'
'C:/xampp/htdocs/files/pdf/i-pdf-0034/'
'C:/xampp/htdocs/files/pdf/i-pdf-0034/form/'
'C:/xampp/htdocs/files/pdf/i-pdf-0034/form/data/' #out of search

Return: 返回:

'NULL'

Two main problems: 两个主要问题:

  1. You don't provide a pattern for glob that will return any subdirectories 您没有为将返回任何子目录的glob提供模式
  2. You don't return the result of the recursive call 您不返回递归调用的结果

which means it will never find your target (unless your target happens to be the directory you start in), so it will never return. 这意味着它永远不会找到你的目标(除非你的目标碰巧是你开始的目录),所以它永远不会返回。


Try making the following changes 尝试进行以下更改

public function LocalDirSearch($clouse) {
    $path    = $clouse[0];
    $search = $clouse[1];
    $result = $clouse[2];

    // add a wildcard to the glob pattern so it will match everything under $path
    $dirs   = glob($path . '/*', GLOB_ONLYDIR);

    foreach ($dirs as $dir) {
        if (basename($dir)==$search){
            return $dir;
        }

        // return the result of the recursive call
        if ($subdir = $this->LocalDirSearch([$dir, $search, $result])) {
            return $subdir;
        }
    }
}

Incidentally, break is unnecessary after return , and $result does not appear to be used in the function. 顺便说一句, return后不需要break ,并且$result似乎没有在函数中使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM