[英]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.