Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
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.
function searchdir($filename, $basedir = '')
{
$results = array();
// Check $dir for a trailing slash if it is set
if (strlen($basedir) > 0)
{
if ($basedir{strlen($basedir)-1} != '\\')
{
$basedir .= '\\';
}
}
else
{
$basedir = getcwd();
}
// Iterate through the specified directory
$dh = opendir($basedir);
while ($file = readdir($dh))
{
if ($file != '.' && $file != '..')
{
// I forget
if (strpos($file, $basedir))
{
$name = trim(str_replace($basedir, '', $file));
}
else
{
$name = $file;
}
// If the file is part of the search, index it
if ($name == $filename)
{
$indice = count($results)+1;
$results[$indice]['name'] = $name;
$results[$indice]['path'] = $file;
}
// If the file is a directory, scan it
if (is_dir($file))
{
searchdir($filename, $file);
}
}
}
closedir($dh);
// return results
if (!is_array($results))
{
return false;
}
return $results;
}
Last edited by daedalus__ on Wed Aug 16, 2006 4:36 am, edited 1 time in total.
I didn't know that glob() was slower, that's a good thing to know, but it's sad too
I can't believe that a PHP function is actually slower (a lot slower) than a user defined function.