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.

$_SERVER['PHP_SELF']

The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. 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.

$_SERVER['QUERY_STRING']

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

$_SERVER['SCRIPT_FILENAME']

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.

$_SERVER['PATH_TRANSLATED']

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.

$_SERVER['SCRIPT_NAME']

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.

$_SERVER['REQUEST_URI']

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

__FILE__
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.
Server PHP Version API $_SERVER["PATH_INFO"] $_SERVER["PATH_TRANSLATED"] $_SERVER["PHP_SELF"] $_SERVER["REQUEST_URI"] $_SERVER["SCRIPT_FILENAME"] $_SERVER["SCRIPT_NAME"] $_SERVER["QUERY_STRING"] __FILE__
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/bin-co.com/test/loc.php /test/loc.php hello=world /home/xxxxxxxx/binnyva/bin-co.com/test/loc.php

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.


<?php
header
("content-type:text/plain"); 

$keys = array(
    
"PATH_INFO",
    
"PATH_TRANSLATED",
    
"PHP_SELF",
    
"REQUEST_URI",
    
"SCRIPT_FILENAME",
    
"SCRIPT_NAME",
    
"QUERY_STRING"
);

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

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

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

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