Jump to content

recursive directory search


Liquid Fire

Recommended Posts

I have built a framework and right now am trying to find any type of speed issue/bottleneck and think i found my big one.  I have a recursive directory search function

 

<?php
function check_for_file($directory, $file_name)
{
//get the directory handle
$directory_handle = opendir($directory);

while($resource = readdir($directory_handle))
{
	//make sure it is not a dotted directory
	if(!is_dot($resource))
	{
		if(is_dir($directory . '/' . $resource))
		{
			//we need to call ourself if this is a directory
			$search_result = check_for_file($directory . '/' . $resource, $file_name);

			if($search_result !== false )
			{
				return $search_result;
			}
		}
		else
		{
			//let check to see if this is the file
			if(file_exists($directory . '/' . $file_name))
			{
				return $directory . '/' . $file_name;
			}
		}
	}
}

return false;
}
?>

 

now in there are 6 folders and about 35-40 files to search and out of the total .56 second it takes to load the data from my framework, .40 is taken up inside this function.  does anyone see where i can optimise my code?

Link to comment
https://forums.phpfreaks.com/topic/92537-recursive-directory-search/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.