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 theSCRIPT_FILENAME
server
variable when it's not populated by Apache. This change was made to
comply with the CGI specification thatPATH_TRANSLATED
should only exist ifPATH_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.0 | PHP 4.4.0 | CGI/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.9 | Apache 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.4 | PHP 5.1.4 | Apache 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.2 | PHP 5.2.1 | CGI/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.
<code>
<b>REFERENCE</b>
<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>
</code>