I am too lazy, I know.
Here is recursive way:
<?php
function rec_listFiles( $from = '.')
{
if(! is_dir($from))
return false;
$files = array();
if( $dh = opendir($from))
{
while( false !== ($file = readdir($dh)))
{
// Skip '.' and '..'
if( $file == '.' || $file == '..')
continue;
$path = $from . '/' . $file;
if( is_dir($path) )
$files += rec_listFiles($path);
else
$files[] = $path;
}
closedir($dh);
}
return $files;
}
?>
And here is iterative:
<?php
function listFiles( $from = '.')
{
if(! is_dir($from))
return false;
$files = array();
$dirs = array( $from);
while( NULL !== ($dir = array_pop( $dirs)))
{
if( $dh = opendir($dir))
{
while( false !== ($file = readdir($dh)))
{
if( $file == '.' || $file == '..')
continue;
$path = $dir . '/' . $file;
if( is_dir($path))
$dirs[] = $path;
else
$files[] = $path;
}
closedir($dh);
}
}
return $files;
}
?>
Searching for a file in a directory tree:
<?php
function rec_listFiles( $fileName, $from = '.', &$pathOut)
{
if ($pathOut != '')
return true;
else
{
if(! is_dir($from))
return false;
if( $dh = opendir($from))
{
while( false !== ($file = readdir($dh)))
{
// Skip '.' and '..'
if( $file == '.' || $file == '..')
continue;
$path = $from . '/' . $file;
if( is_dir($path) )
{
//echo $path.'<p/>';
rec_listFiles($fileName, $path, $pathOut);
}
else
{
if ($fileName == $file)
{
$pathOut = $path;
}
}
}
closedir($dh);
}
if ($pathOut == '')
return false;
else
return true;
}
}
$pom = '';
if (rec_listFiles('mod_newsflash.php', '.', $pom))
echo $pom;
else
echo 'Could not find file.';
?>