在目录和子目录中递归搜索文件的方法找不到该文件

Method to recursively search for a file in directory and subdirectories doesn't find the file

我写了一个函数来在特定目录及其子目录中查找文件。您可以输入带或不带扩展名的文件名(basename 与 filename)。第二个参数是要搜索的路径。文件在给定目录中时会找到,但是,如果它在子目录中则找不到。我尝试利用函数的递归调用来查看子目录。有什么想法我想念的吗?

     public function findFileInPathRecursive($file_to_find, $path) {

        if (substr($path, -1) != "/") $path .= "/"; // Add slash at path's end

        $files = scandir($path);

        foreach($files as $file) {

            if (substr($file, 0, 1) == ".") continue; // skip if "." or ".."

            if (is_dir($path.$file)) { // if "file" is a directory, call self with the new path

                $new_path = $path.$file;

                return $this->findFileInPathRecursive($file_to_find, $new_path);

            }
            else {

                if (pathinfo($file)['basename'] == $file_to_find OR pathinfo($file)['filename'] == $file_to_find) {
                    
                    return $path.$file;

                }

            }

        }

        return false;   

    }
   * Search recusively for files in a base directory matching a glob pattern.
   * The `GLOB_NOCHECK` flag has no effect.
   * @param  string $base Directory to search
   * @param  string $pattern Glob pattern to match files
   * @param  int $flags Glob flags from https://www.php.net/manual/function.glob.php
   * @return string[] Array of files matching the pattern
    
    
    
        function glob_recursive($base, $pattern, $flags = 0) {
            $flags = $flags & ~GLOB_NOCHECK;    
            if (substr($base, -1) !== DIRECTORY_SEPARATOR) {
                $base .= DIRECTORY_SEPARATOR;
            }
            $files = glob($base.$pattern, $flags);
            if (!is_array($files)) {
                $files = [];
            }
            $dirs = glob($base.'*', GLOB_ONLYDIR|GLOB_NOSORT|GLOB_MARK);
            if (!is_array($dirs)) {
                return $files;
            }   
            foreach ($dirs as $dir) {
                $dirFiles = glob_recursive($dir, $pattern, $flags);
                $files = array_merge($files, $dirFiles);
            }
            return $files;
        }
        
        $files = glob($base.$pattern, $flags);
        $files = $files !== false ? $files : [];

我相信 return $this->findFileInPathRecursive($file_to_find, $new_path); 会在第一个子文件夹不包含该文件时导致早期 return。

您可以通过仅在找到文件时 returning 来避免。

if($found = $this->findFileInPathRecursive($file_to_find, $new_path)) {
    return $found;
}

     public function findFileInPathRecursive($file_to_find, $path) {

        if (substr($path, -1) != "/") $path .= "/"; // Add slash at path's end

        $files = scandir($path);

        foreach($files as $file) {

            if (substr($file, 0, 1) == ".") continue; // skip if "." or ".."

            if (is_dir($path.$file)) { // if "file" is a directory, call self with the new path

                $new_path = $path.$file;

                // We only want to return file 
                if($found = $this->findFileInPathRecursive($file_to_find, $new_path)) {
                    return $found;
                }

            }
            else {

                if (pathinfo($file)['basename'] == $file_to_find OR pathinfo($file)['filename'] == $file_to_find) {
                    
                    return $path.$file;

                }

            }

        }

        return false;   

    }