tags:

views:

190

answers:

7

Hello, say i have a file /var/www/foo/test.php

how do i find out it's path from within it. i'm trying to create an "Add To Include Path" action, and for that i need absolute paths.

+1  A: 

This will be provided as a server variable:

$_SERVER["DOCUMENT_ROOT"];
Don Werve
+1  A: 
this_file = $_SERVER["SCRIPT_NAME"];
MarkusQ
+3  A: 

Note: I am leaving my original answer intact, but don't use it. The solutions involving the __FILE__ constant are preferred.

You can use $_SERVER['DOCUMENT_ROOT'] to find the path to the current script. If you need the name of the .php file included, add on $_SERVER["SCRIPT_NAME"], like this:

$_SERVER['DOCUMENT_ROOT'].$_SERVER["SCRIPT_NAME"];
Sidnicious
That might work in this instance, but in general, a PHP script doesn't need to be in the docroot tree. __FILE__ is a better option.
dirtside
Thank you, dirtside. I'm learning something today too! I need to brush up on my PHP before tryingng to answer questions like this.
Sidnicious
+5  A: 

You can use the magic constant, __FILE__

http://us2.php.net/manual/en/language.constants.predefined.php

nickf
+1  A: 

Try:

dirname(__FILE__);

It will give you the directory in which your currently file is located. File has the full path to your file: http://php.net/language.constants.predefined

Darryl Hein
+4  A: 
realpath(dirname(__FILE__))
vartec
+1 for thinking this a bit further, but it doesn't really matter what the realpath is, will it?
nickf
realpath might be handy if you wanted one level up (by appending '/../') but not on it's own.
Ross
+1  A: 
__FILE__ outputs exactly what you need.