在目录中递归搜索所有文件夹路径 [英] Recursively Search within a directory for all folder paths

查看:101
本文介绍了在目录中递归搜索所有文件夹路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图递归地在目录中搜索子目录的任何目录中的所有子目录。基本上所有文件夹都从根目录开始,我需要将文件复制到找到的所有文件夹以及主根文件夹中。我该怎么做?

I am trying to recursively search through a directory for all sub-directories within any directories of sub-directories. Basically all folders starting at the root directory, and I need to copy a file to all folders found as well as in the main root folder. How can I do this?

这是我到目前为止的内容,但是它需要完全递归,以便获取该根目录内的所有文件夹以及子目录内的文件夹,和其中的文件夹,永无休止的搜索,直到没有更多的文件夹为止。

Here's what I have so far, but it needs to be recursive completely so that it gets all folders within that root, and folders within subs, and folders within that, neverending search, until there are no more folders left...

@copy($extendVars['dir'] . '/index.php', $real_extendpath . '/index.php');

$dh = @opendir($real_extendpath);
while (false !== ($obj = readdir($dh)))
{
    if ($obj == '.' || $obj == '..')
        continue;

    if (is_dir($real_extendpath . '/' . $obj))
        @copy($extendVars['dir'] . '/index.php', $real_extendpath . '/' . $obj . '/index.php');
}

closedir($dh);


推荐答案

只有目录才能在文件系统上递归-使用 RecursiveDirectoryIterator 和来自标准PHP库的朋友( docs )。

Recursing over the filesystem for only the directories can be super-easy using the RecursiveDirectoryIterator and friends from the Standard PHP Library (docs).

一个基本示例如下:

$directories = new RecursiveIteratorIterator(
    new ParentIterator(new RecursiveDirectoryIterator($directory_to_iterate)), 
    RecursiveIteratorIterator::SELF_FIRST);

foreach ($directories as $directory) {
    // Do your work here
}






对于您的特殊需求, //在这里工作可能是就像下面的代码片段一样简单。


For your particular needs, the // Do your work here could be as simple as the following snippet.

copy($extendedVars['dir'] . '/index.php', $directory . '/index.php');

这篇关于在目录中递归搜索所有文件夹路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆