Path of the Current File in PHP

There are various methods of find the path of the current file in PHP. But the problem is to find one that is consistent across all servers. The following is a list of the most commonly used methods to find the location of the current file. The definitions are taken from the PHP manual(Predefined Variables) and are modified slightly.


The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address would be /test.php/ The __FILE__ constant contains the full path and filename of the current (i.e. included) file.

If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.


The query string, if any, via which the page was accessed.


The absolute pathname of the currently executing script.

Note: If a script is executed with the CLI, as a relative path, such as file.php or ../file.php, $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user.


Filesystem- (not document root-) based path to the current script, after the server has done any virtual-to-real mapping.

Note: As of PHP 4.3.2, PATH_TRANSLATED is no longer set implicitly under the Apache 2 SAPI in contrast to the situation in Apache 1, where it's set to the same value as the SCRIPT_FILENAME server variable when it's not populated by Apache. This change was made to comply with the CGI specification that PATH_TRANSLATED should only exist if PATH_INFO is defined.

Apache 2 users may use AcceptPathInfo = On inside httpd.conf to define PATH_INFO.


Contains the current script's path. This is useful for pages which need to point to themselves. The __FILE__ constant contains the full path and filename of the current (i.e. included) file.


The URI used to access this page; for instance, '/index.html'. Includes the query string.

The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path whereas in older versions it contained relative path under some circumstances.
Microsoft-IIS/5.0PHP 4.4.0CGI/FastCGI /test/loc.php H:\\test\\loc.php /test/loc.php     /test/loc.php hello=world H:\test\loc.php
Apache/2.0.52 (Fedora)PHP 4.3.9Apache 2.0 Handler   /var/www/htdocs/test/loc.php /test/loc.php /test/loc.php?hello=world /var/www/htdocs/test/loc.php /test/loc.php hello=world /var/www/htdocs/test/loc.php
Apache/2.0.55 (Unix) mod_ssl/2.0.55 OpenSSL/0.9.7a PHP/5.1.4PHP 5.1.4Apache 2.0 Handler     /test/loc.php /test/loc.php?hello=world /var/www/htdocs/test/loc.php /test/loc.php hello=world /var/www/htdocs/test/loc.php
Apache/2.0.54 (Unix) PHP/4.4.4 mod_ssl/2.0.54 OpenSSL/0.9.7e mod_fastcgi/2.4.2 DAV/2 SVN/1.3.2PHP 5.2.1CGI/FastCGI     /test/loc.php /test/loc.php?hello=world /home/binnyva/ /test/loc.php hello=world /home/xxxxxxxx/binnyva/

If you have access to a server that is not in the above list, please the below code to generate the results and send the result to me - I will include it here.


$keys = array(

$info_row "<tr><td>$_SERVER[SERVER_SOFTWARE]</td><td></td><td></td>\n";
"Path Information for $_SERVER[SERVER_SOFTWARE]\n\n";

$keys as $key) {
'$_SERVER["'.$key.'"] = '.$_SERVER[$key]."\n";
$info_row .= "<td>$_SERVER[$key]</td>\n";

'__FILE__ = '__FILE__;
$info_row .= "<td>".__FILE__."</td>\n</tr>";

"\n\n\n" $info_row;
Subscribe to Feed