如何创建向后循环来查找第一个匹配文件

发布于 2024-09-27 23:10:06 字数 257 浏览 0 评论 0原文

我正在寻找的是如何构建一个函数,该函数采用子文件夹循环遍历它并在目录层次结构中向后查找指定的第一个匹配文件。

一个例子是我可以说我有目录结构: Home / 文件夹 1 / 文件夹 2 / 文件夹 3

我正在寻找文件 style.css。

我想开始指向子文件夹(文件夹 3)并查找 style.css,如果它不存在,它将继续指向父文件夹(文件夹 2),依此类推。但它不应该比文件夹 1 更早。

如果有人知道如何做到这一点,我将非常感激!

What I'm looking for is a how to build a function that takes a child folder an loops through it and backwards in the directory hierarchy to find the first matching file that is specified.

A example is that i lets say i have the directory structure:
Home / Folder 1 / Folder 2 / Folder 3

And I'm looking for the file style.css.

I would like to start pointing to the child folder (Folder 3) and look for the style.css and if it's not there it would continue to the parent folder (Folder 2) and so on. But it should not go further back than Folder 1.

If anybody have a good idea how do to this I would be very grateful!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

寄意 2024-10-04 23:10:06

一种快速而肮脏的方法是:

function traverse_backward($filename, $path, $min_depth) {
    // $path = '/home/user/projects/project1/static/css/'; 
    // $min_depth - the minimum level of the path;
    // $filename - the file name you are looking for, e.g. 'style.css'
    $path_parts = explode('/',$path);
    while (count($path_parts) > $min_depth) {
       $real_path = implode($path_parts,'/').'/';
       if (is_file($real_path.$filename)) {
          return $real_path;
       }
       array_pop($path_parts);
    }

    return false;
}
traverse_backward('t.php', '/home/user/projects/test-www/static/css', 3);

A quick and dirty way would be:

function traverse_backward($filename, $path, $min_depth) {
    // $path = '/home/user/projects/project1/static/css/'; 
    // $min_depth - the minimum level of the path;
    // $filename - the file name you are looking for, e.g. 'style.css'
    $path_parts = explode('/',$path);
    while (count($path_parts) > $min_depth) {
       $real_path = implode($path_parts,'/').'/';
       if (is_file($real_path.$filename)) {
          return $real_path;
       }
       array_pop($path_parts);
    }

    return false;
}
traverse_backward('t.php', '/home/user/projects/test-www/static/css', 3);
段念尘 2024-10-04 23:10:06

对第一个答案的进一步解释:在 PHP 中使用路径时,将路径分解为数组很方便。如果路径位于数组中,则使用路径会更容易。在这种情况下,您可以使用 array_pop() 在循环的每次迭代中删除数组的最后一个元素。然后你可以在路径上使用 implode() 将其放回到字符串中,该字符串可以与文件函数(例如 file_exists())一起使用。

Further explanation for the fist answer: When working with paths in PHP, it is convenient to explode() the path in to an array. It's easier to work with paths if they are in an array. In this case, you use array_pop() to remove the last element of the array with each iteration of the loop. Then you can use implode() on the path to put it back in to a string, the string can be used with file functions such us file_exists().

凶凌 2024-10-04 23:10:06

这是一个简单的递归函数(可能的迭代次数有限,因此不会有太多开销)。伪代码是这样的:

function lookForCss($from) {
  if(from contains css file) {
    return $from;
  }
  //else 
  $from = go up one folder($from);
  return lookForCss($from);
}

This is a simple recursive function (you have a limited amount of possible iterations, so there won't be much overhead). The pseudocode is like this:

function lookForCss($from) {
  if(from contains css file) {
    return $from;
  }
  //else 
  $from = go up one folder($from);
  return lookForCss($from);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文