简体   繁体   中英

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.

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

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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