【问题标题】:Recursively search Directory by name and return path按名称和返回路径递归搜索目录
【发布时间】:2019-10-25 07:12:12
【问题描述】:

我有一个最多 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]);
        }
    }

我制作了这个脚本,但它不能正常工作我不知道它到底缺少什么,它会无限期地运行......没有错误输出......

更新

这个目录结构:

+--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

函数调用:

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

搜索目录的脚本:

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]);  
        }
    }
}

目录中递归打印导航的输出:

'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

返回:

'NULL'

【问题讨论】:

  • return = $dir 中有一个错误(或拼写错误?),但似乎不是无限的。
  • 是的,错了,我删除了'='
  • @Sammitch 我看到了这个,但是它可以返回文件名我没有找到任何像我一样的脚本,它适用于嵌套目录。

标签: php recursion directory


【解决方案1】:

两个主要问题:

  1. 您没有为 glob 提供将返回任何子目录的模式
  2. 你没有返回递归调用的结果

这意味着它永远找不到你的目标(除非你的目标恰好是你开始的目录),所以它永远不会返回。


尝试进行以下更改

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;
        }
    }
}

顺便说一句,break 在return 之后是不必要的,而$result 似乎没有在函数中使用。

【讨论】:

  • $path 是初始路径目录,$search 是类似字符串的模式pdf_files...
  • 您在寻找与$search 完全匹配的对象吗?
  • 是的,如果 $search = to pdf_files 我需要获取第一个具有此名称的目录,因为我使用 basename() 进行比较。
  • 返回 NULL 或 this => ''
  • 我已经更新了所有可能出现问题的示例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-14
  • 2013-09-24
  • 2019-12-23
  • 1970-01-01
  • 2023-03-27
  • 2023-04-04
相关资源
最近更新 更多