Path of the Current File in PHP


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.

* __FILE__ returns the filename of the script that is currently being run.
* dirname returns the directory of a given filename
* basename() returms the filename component of path. To put it better: it strips any “parent” directories off a given string.

Example: Suppose you run /home/httpd/html/index.php, which contains

Now __FILE__ gives “/home/httpd/html/index.php”, dirname(__FILE__) gives “/home/httpd/html” and basename(dirname(__FILE__)) gives “html”. So basename() stripped the “parent” directory “/home/httpd/” off the string “/home/httpd/html”. Don’t be confused of the fact that /home/httpd/html is a directory, but basename is supposed to operate on “filenames”. Internally, a directory is just another file, so a directory name is treated like a filename.


$keys = array(

$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;
Share this Post:
Digg Google Bookmarks reddit Mixx StumbleUpon Technorati Yahoo! Buzz DesignFloat Delicious BlinkList Furl

Comments are closed.