Tuesday, March 23, 2010

Path of the Current File in PHP

SkyHi @ Tuesday, March 23, 2010

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
) 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
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.


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


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.


(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.


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.


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

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.phpH:\\test\\loc.php/test/loc.php

Apache/2.0.52 (Fedora)PHP 4.3.9Apache 2.0

Apache/2.0.55 (Unix) mod_ssl/2.0.55 OpenSSL/0.9.7a PHP/5.1.4PHP
Apache 2.0 Handler

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.2
PHP 5.2.1CGI/FastCGI


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.

<a href="http://www.bin-co.com/php/articles/current_file_path.php">http://www.bin-co.com/php/articles/current_file_path.php</a>