简体   繁体   English

如何从多个不同的目录检索所有文本文件?

[英]How to retrieve all text files from multiple different directories?

You may have heard of the glob method but that only manages to retrieve files from the directory that the file containing the method is located on - only one directory. 您可能听说过glob方法,但是该方法只能从包含该方法的文件所在的目录(仅一个目录)中检索文件。

This is an example of the code that I am using: 这是我使用的代码示例:

<?php

foreach (glob("*.txt") as $filename) 
{
    $time = filemtime($filename);
    $files[$time] = $filename;
}

krsort($files);

foreach ($files as $file) {
echo $file;

}

?>

What happens here is that all of the text files in the current directory are retrieved, they are then sorted by order of date modified and are then echoed out onto the page. 此处发生的是,将检索当前目录中的所有文本文件 ,然后按修改日期顺序对其进行排序,然后将其回显到页面上。

The problem with this is that I don't just want to retrieve text files from the one directory. 问题是我不只想从一个目录中检索文本文件。

How would I change this so that I can retrieve files from multiple directories of my choice all from one page - so I can echo out all of the text files from the multiple directories onto one page rather than only echoing out the text files from one directory? 我将如何更改此设置,以便可以从一个页面中从我选择的多个目录中检索文件-因此我可以将多个目录中的所有文本文件回显到一页上,而不是仅从一个目录中回显文本文件?

I believe I would need to store all the directories I want to glob into an array but I am not sure how to retrieve it. 我相信我需要将我想要遍历的所有目录存储到一个数组中,但是我不确定如何检索它。

Here is an example stolen from the page in my comment above 这是我上面评论中的页面被盗的示例

<?php

$Directory = new RecursiveDirectoryIterator('path/to/project/');
$Iterator = new RecursiveIteratorIterator($Directory);
$Regex = new RegexIterator($Iterator, '/^.+\.txt$/i', RecursiveRegexIterator::GET_MATCH);

?>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM