Get the Current Script File Name in PHP

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 briefly understand all the parameters & methods 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. The webserver creates all this information.

  • 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, and the only difference is its path is absolute.

  • SCRIPT_NAME:

    It Contains the current script’s path. This is useful for pages that need to point to themselves.

  • REQUEST_URI:

    The URI was given to access the page’s location, for instance, /index.html.

Now let’s use all the above commands together to get the file name.

  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 example, `http://example.com/parentFolder/child.php` would be `/parentFolder/child.php`.
  1. $_SERVER['SCRIPT_FILENAME']:

    parent file full URL with a file extension

  2. $_SERVER['REQUEST_URI']:

    parent file parent folder name with

The inbuilt PHP function basename() 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.

Sample Code to Get the Current Script File Name in PHP

<?php
echo "\$_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";
?>

Output:

get the current script file name in PHP

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.