Page 1 of 1

find file

Posted: Wed Jan 09, 2008 3:56 am
by devendra-m
You can use following code to find full path of the file you wanted

Code: Select all

<?	
	$main="test";
	define('ROOT_PATH',str_replace('//','/',ereg_replace("((.*)/?)", "\\2", $_SERVER['DOCUMENT_ROOT']).(trim($main)!=''?"/".$main:'')));
	function traverse($root,$destination)
	{
		$handle = opendir($root);
		while (false !==($file=readdir($handle))) 
			if($file != '.' && $file != '..')
			{
				$full_path=$root."/".$destination;				
				if(file_exists($full_path))				
					return $full_path;
		
				$path=$root."/".$file;
				if(is_dir($path))
					if($full_path=traverse($path,$destination))
						return $full_path;
			}
	}
	function absolutePathServer($destination)
	{
		if($full_path=traverse(ROOT_PATH,$destination))
			return $full_path;
		else 
			return $destination;
	}
	function absolutePathClient($destination)
	{
		if($full_path=traverse(ROOT_PATH,$destination))
			return "http://".str_replace('//','/',str_replace($_SERVER['DOCUMENT_ROOT'],$_SERVER['HTTP_HOST']."/", $full_path));
		else 
			return $destination;
	}
?>

Posted: Wed Jan 09, 2008 8:16 am
by Jenk
alternatively:

Code: Select all

realpath($destination);

function absolutePathClient ($destination) {
  if (strpos(realpath($destination), $_SERVER['DOCUMENT_ROOT']) === 0) {
    return substr(realpath($destination), strlen($_SERVER['DOCUMENT_ROOT']));
  } else {
    return false; // or throw new Exception('File outside of document root'); or whatever.
  }
}