PHP include ladder to find include path.

I needed a way to be able to include particular files from files that are themselves included. because the working directory was not necessarily the same, so I needed a way to find what the actual include path should be. The following code snippet finds a specific folder or file and returns the ladder (relative path) to get to it.

function find_path($loc,$dir=0){
	for($i = 0; $i < 10; $i++){
		if($dir){
			if(is_dir($ladder.$loc)){
				return $ladder;
				break;
			}
		}elseif(is_file($ladder.$loc)){
			return $ladder;
			break;
		}
		$ladder .='../';
	}
}

This code is NOT user-input safe: any checks you do should be hard coded otherwise you might as well just give out your passwords.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>