Return to Snippet

Revision: 59349
at September 1, 2012 13:39 by VisionHive


Initial Code
function FFIND($f,$p=null,$l=1000)
{	// Recursively find a file $f in directory $p (compare up to $l files)
	// Returns the full path of the first occurrence (relative to current directory)
	// (c) Peter Mugane Kionga-Kamau http://www.visionhive.com
	// Free for unrestricted use with this notice and description unaltered.
	$cd=$p==null?getcwd():$p;
	if(substr($cd,-1,1)!="/")$cd.="/";
	if(is_dir($cd))
	{
		$dh=opendir($cd);
		while($fn=readdir($dh))
		{	// traverse directories and compare files:
			if(is_file($cd.$fn)&&$fn==$f){closedir($dh);return $cd.$fn;}
			if($fn!="."&&$fn!=".."&&is_dir($cd.$fn)){$m=ffind($f,$cd.$fn,$l);if($m){closedir($dh);return $m;}}
		}
		closedir($dh);
	}
	return false;
}

Initial URL
http://www.visionhive.com

Initial Description
This function searches the current working directory (or any other directory path that you specify with parameter $p) for a file, named $f, and returns the full path and filename of the first occurrence, or false if it is either not found or the maximum number of comparisons is reached. The return value is relative to the executing script, so you can safely pass this function to other file-handling functions, e.g. fopen(FFIND("my.file")); or file(FFIND("my.file"));

Initial Title
Find a file (including subdirectory search)

Initial Tags
file, search, find

Initial Language
PHP