PHP | Get the current script filename

Image by Lawrence Monk from Pixabay

__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'.

  1. $_SERVER[‘SCRIPT_NAME’]:
    A parent file name with a file extension
  2. $_SERVER[‘PHP_SELF’]:
    Parent file relative URL with the file extension. For eg, http://example.com/parentFolder/child.php would be /parentFolder/child.php.
  3. $_SERVER[‘SCRIPT_FILENAME’]:
    parent file full URL with a file extension
  4. $_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.

  1. basename(__FILE__)
    Current file name with PHP file extension.
  2. basename(__FILE__, '.php'):
    Current file name without PHP file extension.
  3. basename($_SERVER[‘PHP_SELF’], “.php”):
    Current file name without PHP file extension.
  4. basename($_SERVER[‘PHP_SELF’]):
    Current file name with PHP file extension.
  5. pathinfo(__FILE__, PATHINFO_FILENAME):
    Current file name without PHP file extension.
<?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.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Shraddha Paghdar

Shraddha Paghdar

Javascript Full-stack developer with a passion for building highly scalable and performant apps. || https://noob2geek.in/