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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.