Recursive Find File

Recursively looks for a file in directories in the current a top-level from the calling script. Will return false if the file cannot be found.

function find_file($file, $prev_path = '')
{
	$new_path = ($prev_path == '') ? dirname(__FILE__) : dirname($prev_path);
	
	if ($prev_path == $new_path)
		return false;
	
	$full_path = $new_path . DIRECTORY_SEPARATOR . $file; 
	
	return (file_exists($full_path)) ? $full_path : find_file($file, $new_path);
}

Usage

$file = find_file('file_in_a_higher_directory_but_dont_know_where.php'); if ($file) include($file); else echo "File Not Found!";


Comments

Add your comment