Hekp me improve this function
Posted: Wed Aug 16, 2006 2:46 am
I wrote this function because I needed it. It could probably be improved. Suggestions?
P.S. PHP5
I completely forgot to mention what it does!
It scans all directories and sub-directories and sub-sub-directories, and so on, in the basepath you provide and returns an array with the path of matching files.
P.S. PHP5
I completely forgot to mention what it does!
It scans all directories and sub-directories and sub-sub-directories, and so on, in the basepath you provide and returns an array with the path of matching files.
Code: Select all
function searchdir($filename, $basedir = '', $onlydir = FALSE)
{
$results = array();
// Check $dir for a trailing slash if it is set
if (strlen($basedir) > 0)
{
if ($basedir[strlen($basedir)-1] != '\\')
{
$basedir .= '\\';
}
}
// Check if the user wants to scan only directories
if ($onlydir == 1)
{
$tree = glob("$basedir*", GLOB_ONLYDIR);
}
else
{
$tree = glob("$basedir*");
}
// Iterate through the specified directory
foreach($tree as $file)
{
$name = trim(str_replace($basedir, '', $file));
if ($name == $filename)
{
$results[1] = $filename;
}
searchdir($filename, $file, $onlydir);
}
// return results
if (!is_array($results))
{
return false;
}
return $results;
}
$time_begin = microtime(true);
print '<pre>';
print_r(searchdir('index.php'));
print '</pre>';
$time_end = microtime(true);
echo $time_end - $time_begin;tankies!output wrote: Array
(
[1] => index.php
)
0.00599098205566