Hekp me improve this function

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.

Moderator: General Moderators

User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

write your own function :)
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Post 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)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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.
Yossarian
Forum Contributor
Posts: 101
Joined: Fri Jun 30, 2006 4:43 am

Post 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...
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post 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. :D

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. :D

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;
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I'm actually glad that you did. :D
No worries.
Post Reply