Monday, May 24, 2010

How do I get the file name in PHP?

What is the script needed to get the current file name of the opened php file? Thanks.

How do I get the file name in PHP?
The constant __FILE__ returns something like:


c:\Inetpub\wwwroot\test\phpinfo.php





This actually is the full physical path of the file containing the line.





Combine it with basename function:


basename( __FILE__ )





returns:


phpinfo.php





Lastly, there is a variable called PHP_SELF in $_SERVER. You can access it as:


$_SERVER[ "PHP_SELF" ]





This gives virtual path of the file:


/test/phpinfo.php


No comments:

Post a Comment