简体   繁体   English

在目录中递归搜索所有文件夹路径

[英]Recursively Search within a directory for all folder paths

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

Recursing over the filesystem for only the directories can be super-easy using the RecursiveDirectoryIterator and friends from the Standard PHP Library ( docs ). 使用RecursiveDirectoryIterator和标准PHP库( docs )中的朋友,仅需目录的文件系统上的RecursiveDirectoryIterator就非常容易。

A basic example would look like 一个基本的例子看起来像

$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. 为了满足您的特殊需求, // Do your work here在以下代码段中// Do your work here即可。

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

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

相关问题 递归 chmod/chown/chgrp 目录中的所有文件和文件夹 - Recursively chmod/chown/chgrp all files and folder within a directory 递归重命名目录中的所有子目录 - Rename all subdirectories within a directory recursively 使用 PHP 为目录中的所有 JavaScript 文件递归生成脚本标签 - Recursively generate script tags for all JavaScript files within a directory with PHP 递归将给定目录PHP或命令行中的所有文件和目录大写 - Recursively Capitalize all Files and Directories within a Given Directory PHP or Commandline 如何从目录中递归找到的所有文件中搜索行匹配 - how to search for a line match from all files recursively found in a directory 将一个文件夹中的文件路径递归映射到另一个文件夹 - Recursively mapping file paths in one folder to another folder 递归列出目录中的所有文件 - Recursively listing all files in directory 从文件夹中随机播放视频文件,递归查看所有目录结构树 - Play video file randomly from a folder recursively looking down into all directory structure tree 从Docroot递归搜索.git目录 - Recursively Search for .git Directory From Docroot 按名称和返回路径递归搜索目录 - Recursively search Directory by name and return path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM