Hekp me improve this function
Moderator: General Moderators
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)
http://www.php.net/~helly/php/ext/spl/
(I cannot vouch for its speed)
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm
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.
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; }
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
I was waiting to see how you'd handle it before saying something about the bug.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 ".".
- daedalus__
- DevNet Resident
- Posts: 1925
- Joined: Thu Feb 09, 2006 4:52 pm