Path to an included file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rednival
Forum Newbie
Posts: 4
Joined: Tue Jul 08, 2008 11:24 am

Path to an included file

Post by rednival »

I am including a file that is on the include_path, but I would like to get full, physical path of the file. For instance, directory '/share/includes' is on my include_path and has the file foo.php in it. I include it using:

Code: Select all

require('foo.php');
I want a way to convert 'foo.php' to '/share/includes/foo.php' so I know where the file came from.

Thoughts?
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Path to an included file

Post by twinedev »

Try the following:

Code: Select all

$strFile = 'PEAR.php';
foreach (explode(':',get_include_path()) as $strCurPath) {
	$strFullPath = realpath($strCurPath).'/'.$strFile;
	if (file_exists($strFullPath)) {
		echo "FOUND FILE AT: ",$strFullPath;
		break;
	}
	$strFullPath = FALSE;
}
if (!$strFullPath) {
	echo "ERROR: Count not find file in the path";
}
It will report the first found location, in the order that the includes (as far as I know) looks for it. In this sample, my server, PEAR.php is found in both /usr/lib/php and /usr/local/lib/php, but reported it in the first one found.

-Greg
rednival
Forum Newbie
Posts: 4
Joined: Tue Jul 08, 2008 11:24 am

Re: Path to an included file

Post by rednival »

EXACTLY what I needed. Thanks.
twinedev wrote:Try the following:

Code: Select all

$strFile = 'PEAR.php';
foreach (explode(':',get_include_path()) as $strCurPath) {
	$strFullPath = realpath($strCurPath).'/'.$strFile;
	if (file_exists($strFullPath)) {
		echo "FOUND FILE AT: ",$strFullPath;
		break;
	}
	$strFullPath = FALSE;
}
if (!$strFullPath) {
	echo "ERROR: Count not find file in the path";
}
It will report the first found location, in the order that the includes (as far as I know) looks for it. In this sample, my server, PEAR.php is found in both /usr/lib/php and /usr/local/lib/php, but reported it in the first one found.

-Greg
Post Reply