Page 2 of 2
Posted: Wed Aug 16, 2006 5:11 am
by Weirdan
write your own function

Posted: Wed Aug 16, 2006 8:52 am
by Yossarian
Using PHP5, what do you think of the inbuilt DirectoryIterator class from the SPL?
http://www.php.net/~helly/php/ext/spl/
(I cannot vouch for its speed)
Posted: Wed Aug 16, 2006 8:54 am
by feyd
The SPL is somewhat slow compared to several more basic alternatives, depending on the class(es) used. But the SPL isn't really about performance, it's more about OOP.
Posted: Wed Aug 16, 2006 12:18 pm
by daedalus__
If I remember correctly SPL is even slower than glob. Since I this is a pretty specialized function I thought I would try to make it as fast as possible.
Posted: Wed Aug 16, 2006 2:30 pm
by daedalus__
I just realised that the function doesn't work correctly! I worked on it after midnight last night and was pretty tired so I am not too surprised. It was working at one point. :- /
EDIT: I'm going to re-post it when it works.
Posted: Wed Aug 16, 2006 5:36 pm
by Yossarian
Yossarian wrote:Using PHP5, what do you think of the inbuilt DirectoryIterator class from the SPL
Thanks for your replies. I will test that with WinCacheGrind and my new cycling shorts - after I have shaved my legs of course...
Posted: Wed Aug 16, 2006 8:29 pm
by daedalus__
There were some things about this function that I am surprised nobody pointed out.
Specifically, it was broken and wouldn't go more than one level past the root directory, and I used getcwd() instead of ".".
^^^^^^^^^^^^^^^^
Anyways, here is the new version. It works and is faster than using glob, dir, or SPL.
I'm probably going to add a glob-like search thing to it, maybe some other things, whatever I can think of to make it neat.
I tested a dh, glob, and dir based function and this one is about 20% faster than the others.
I welcome any suggestions anyone has.
Feel free to use it yourself.
searchdir.func.php wrote:
Code: Select all
function searchdir($filename, $basedir = '.')
{
$results = array();
// Iterate through the specified directory
$dh = opendir($basedir);
while ($file = readdir($dh))
{
if ($file != '.' && $file != '..')
{
$path = $basedir.'/'.$file;
if ($file == $filename)
{
$results[] = $path;
}
if (is_dir($path))
{
searchdir($filename, $path);
}
}
}
closedir($dh);
// return results
if (!count($results) > 0)
{
return false;
}
return $results;
}
Posted: Wed Aug 16, 2006 8:32 pm
by feyd
Daedalus- wrote:There were some things about this function that I am surprised nobody pointed out.
Specifically, it was broken and wouldn't go more than one level past the root directory, and I used getcwd() instead of ".".
I was waiting to see how you'd handle it before saying something about the bug.
Posted: Wed Aug 16, 2006 8:39 pm
by daedalus__
I'm actually glad that you did.

No worries.