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

Hekp me improve this function

Post by daedalus__ »

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.

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;
output wrote: Array
(
[1] => index.php
)
0.00599098205566
tankies!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

If you mean improve as in "make it go faster" then replace glob() with a combination of opendir() and readdir(). glob() is slow.
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I'll take any suggestion for improvement. Though, faster would be nicer.

glob() is really that slow?

EDIT: hasty reponse was nice. ^^
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Not sure what you wanted to do here, but I think you meant:

Code: Select all

if ($basedir{strlen($basedir)-1]} != '\\')
not:

Code: Select all

if ($basedir[strlen($basedir)-1] != '\\')
Is that correct?
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

I don't know what curly braces do... lol
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Daedalus- wrote:I don't know what curly braces do... lol
Check the examples here: http://us2.php.net/manual/en/function.substr.php
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

For some reason I completely neglected to see that everytime I read that part of the manual!!!!
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Changed from glob() to directory handlers and got about a 20% performance increase. :)

Any more suggestions?

Code: Select all

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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

// If the file is a directory, scan it
                        if (is_dir($file))
                        {
                                searchdir2($filename, $file);
                        }
Fatal error, searchdir2 is not defined.

Code: Select all

if ($name == $filename)
                        {
                                $indice = count($results)+1;
                                $results[$indice]['name'] = $name;
                                $results[$indice]['path'] = $file;
                        }
why count()+1 ? Don't you like ye ole' zero-based arrays?
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Weirdan wrote: Fatal error, searchdir2 is not defined.
searchdir2 was a typo, sorry.
Weirdan wrote: why count()+1 ? Don't you like ye ole' zero-based arrays?
wha?

count()+1 because [] wouldn't work how i wanted it to?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

why +1?
User avatar
daedalus__
DevNet Resident
Posts: 1925
Joined: Thu Feb 09, 2006 4:52 pm

Post by daedalus__ »

Doh!!!!

Tiredness.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

I didn't know that glob() was slower, that's a good thing to know, but it's sad too :cry:
I can't believe that a PHP function is actually slower (a lot slower) than a user defined function.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

glob has much more features than Daedalus' function
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Weirdan wrote:glob has much more features than Daedalus' function
Yeah, that's right, but what if I don't need all these handy dandy features?
Post Reply