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 :)
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.