PHP | Get the current script filename
Your script may need the current file name with the directory name in which it is currently executing. In this tutorial article, we will discuss how to get the filename of the current script inside the project.
PHP provides various ways to find out the current file name. First, we will understand all the parameters & methods in short and then combine them to get the result.

__FILE__:
PHP provides 9 magical constants which are used on the basis of their use. These constants are created by various extensions. All of these constants are resolved during compile time. __FILE__
is one of such a magic constant that gives you the filesystem path to the current .php file.
$_SERVER:
$_SERVER is an array that contains information about headers, paths, and script locations. All this information is created by the webserver.
PHP_SELF:
PHP_SELF is a variable used to get the filename of the currently executing script. It is relative to the document root. When the user runs this command in the command line, it will print the information about the script name.
SCRIPT_FILENAME:
This is a variable used to get the filename of the currently executing script, the only difference is its path is absolute.
SCRIPT_NAME:
Contains the current script’s path. This is useful for pages that need to point to themselves.
REQUEST_URI:
The URI which was given in order to access the location of the page; for instance, ‘/index.html
'.
Now let’s use all the above commands together to get the file name:
- $_SERVER[‘SCRIPT_NAME’]:
A parent file name with a file extension - $_SERVER[‘PHP_SELF’]:
Parent file relative URL with the file extension. For eg, http://example.com/parentFolder/child.php would be /parentFolder/child.php. - $_SERVER[‘SCRIPT_FILENAME’]:
parent file full URL with a file extension - $_SERVER[‘REQUEST_URI’]:
parent file parent folder name with
basename()
This inbuilt PHP function returns the base name of a file if the path of the file is provided as a parameter to the basename() function.
- basename(__FILE__)
Current file name with PHP file extension. - basename(__FILE__, '.php'):
Current file name without PHP file extension. - basename($_SERVER[‘PHP_SELF’], “.php”):
Current file name without PHP file extension. - basename($_SERVER[‘PHP_SELF’]):
Current file name with PHP file extension. - pathinfo(__FILE__, PATHINFO_FILENAME):
Current file name without PHP file extension.
Let’s run all the above commands
<?phpecho "\$_SERVER['SCRIPT_NAME']: ";echo $_SERVER['SCRIPT_NAME'];echo "\n";
echo "\$_SERVER['PHP_SELF']: ";echo $_SERVER['PHP_SELF'];echo "\n";echo "\$_SERVER['SCRIPT_FILENAME']: ";echo $_SERVER['SCRIPT_FILENAME'];echo "\n";echo "basename(__FILE__): ";echo basename(__FILE__);echo "\n";echo "basename(__FILE__, '.php'): ";echo basename(__FILE__, '.php');echo "\n";echo "basename(\$_SERVER['PHP_SELF'], '.php'): ";echo basename($_SERVER['PHP_SELF'], '.php');echo "\n";echo "basename(\$_SERVER['PHP_SELF']): ";echo basename($_SERVER['PHP_SELF']);echo "\n";echo "pathinfo(__FILE__, PATHINFO_FILENAME): ";echo pathinfo(__FILE__, PATHINFO_FILENAME);echo "\n";?>
Conclusion
There are various functions available to get the name of the current file. You can choose depending on your requirement.