php – How do i fetch my file’s absolute path – Education Career Blog

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.

,

You can use the magic constant, __FILE__

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

,

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";

,

realpath(dirname(__FILE__))

,

This will be provided as a server variable:

$_SERVER"DOCUMENT_ROOT";

,

this_file = $_SERVER"SCRIPT_NAME";

,

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

,

__FILE__ outputs exactly what you need.

Leave a Comment