I need something to read all files in a directory and all files in child directories and any directories below this.
Could someone provide a snippet for me.
Thanks :)
Useful suggestions already posted: pointed to the simple scandir/is_dir route and showed the traditional opendir/readdir recursion, while correctly steered the thread toward SPL recursive iterators. For small trees the simple functions work, but for robustness and performance on large or deep trees use the SPL iterators and stream results instead of building big arrays.
Here is a compact, memory-friendly pattern that filters unwanted dirs and yields file paths as you iterate:
<?php
function iterate_files($start, array $skipDirs = ['.git','vendor']) {
$base = new RecursiveDirectoryIterator($start, FilesystemIterator::SKIP_DOTS);
$filter = new RecursiveCallbackFilterIterator($base, function($current) use ($skipDirs) {
if ($current->isDir() && in_array($current->getFilename(), $skipDirs, true)) {
return false; // don't descend into skipped dirs
}
return true;
});
$it = new RecursiveIteratorIterator($filter);
foreach ($it as $info) {
if ($info->isFile()) {
yield $info->getPathname();
}
}
} Example usage:
foreach (iterate_files(__DIR__) as $path) {
echo $path, PHP_EOL;
} Practical notes: fix the common opendir bug in 's code by joining paths with DIRECTORY_SEPARATOR or rtrim($dir, '/\') . DIRECTORY_SEPARATOR . $file; always skip '.' and '..'; wrap iterator creation in try/catch for permission errors; avoid following symlinks if you might hit cycles; and use generators (above) to keep memory low. If you need max depth, call $it->setMaxDepth($n) on the RecursiveIteratorIterator.
Jump to Post— blocblue 238A hint would be to use the
scandirandisdirPHP method to write a recursive method...Post what you manage to write, and I'll happily help.
Jump to Post— kireol 10<?php function recurseDir($dir) { if(is_dir($dir)) { if($dh = opendir($dir)) { while(($file = readdir($dh)) !== false) { if($file != '.' && $file != '..') { if(!is_dir($dir . $file)) { if(is_dir($file)) { echo "Directory->".$dir . $file."<br/>"; } else { echo "File->".$dir . $file."<br/>"; } } else { if(is_dir($file)) { …
A hint would be to use the scandir and isdir PHP method to write a recursive method...
Post what you manage to write, and I'll happily help.
<?php
function recurseDir($dir)
{
if(is_dir($dir))
{
if($dh = opendir($dir))
{
while(($file = readdir($dh)) !== false)
{
if($file != '.' && $file != '..')
{
if(!is_dir($dir . $file))
{
if(is_dir($file))
{
echo "Directory->".$dir . $file."<br/>";
}
else
{
echo "File->".$dir . $file."<br/>";
}
}
else
{
if(is_dir($file))
{
echo "Directory->".$dir . $file."<br/>";
}
else
{
echo "File->".$dir . $file."<br/>";
}
}
}
}
}
closedir($dh);
}
}
recurseDir(".");
?>
Works for me - have you looked at the DirectoryIterator? New(ish) function for php
http://uk3.php.net/directoryiterator
Nice examples here:
http://www.phpro.org/tutorials/Introduction-to-SPL-DirectoryIterator.html#1
Try this:
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));
foreach($files as $file) {
echo $file . "<br />";
} AND techno info here:
http://www.php.net/~helly/php/ext/spl/directorytreeiterator_8inc-source.html
It's truly beautiful.
Try this
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));
foreach($files as $file) {
echo $file . "<br />";
} wow, that's pretty cool. didnt know that existed.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.