Get the current script file name

PhpFile

Php Problem Overview


If I have PHP script, how can I get the filename from inside that script?

Also, given the name of a script of the form jquery.js.php, how can I extract just the "jquery.js" part?

Php Solutions


Solution 1 - Php

Just use the PHP magic constant __FILE__ to get the current filename.

But it seems you want the part without .php. So...

basename(__FILE__, '.php'); 

A more generic file extension remover would look like this...

function chopExtension($filename) {
    return pathinfo($filename, PATHINFO_FILENAME);
}

var_dump(chopExtension('bob.php')); // string(3) "bob"
var_dump(chopExtension('bob.i.have.dots.zip')); // string(15) "bob.i.have.dots"

Using standard string library functions is much quicker, as you'd expect.

function chopExtension($filename) {
    return substr($filename, 0, strrpos($filename, '.'));
}

Solution 2 - Php

When you want your include to know what file it is in (ie. what script name was actually requested), use:

basename($_SERVER["SCRIPT_FILENAME"], '.php')

Because when you are writing to a file you usually know its name.

Edit: As noted by Alec Teal, if you use symlinks it will show the symlink name instead.

Solution 3 - Php

See http://php.net/manual/en/function.pathinfo.php

pathinfo(__FILE__, PATHINFO_FILENAME);

Solution 4 - Php

Here is the difference between basename(__FILE__, ".php") and basename($_SERVER['REQUEST_URI'], ".php").

basename(__FILE__, ".php") shows the name of the file where this code is included - It means that if you include this code in header.php and current page is index.php, it will return header not index.

basename($_SERVER["REQUEST_URI"], ".php") - If you use include this code in header.php and current page is index.php, it will return index not header.

Solution 5 - Php

This might help:

basename($_SERVER['PHP_SELF'])

it will work even if you are using include.

Solution 6 - Php

Here is a list what I've found recently searching an answer:

//self name with file extension
echo basename(__FILE__) . '<br>';
//self name without file extension
echo basename(__FILE__, '.php') . '<br>';
//self full url with file extension
echo __FILE__ . '<br>';

//parent file parent folder name
echo basename($_SERVER["REQUEST_URI"]) . '<br>';
//parent file parent folder name with //s
echo $_SERVER["REQUEST_URI"] . '<br>';

// parent file name without file extension
echo basename($_SERVER['PHP_SELF'], ".php") . '<br>';
// parent file name with file extension
echo basename($_SERVER['PHP_SELF']) . '<br>';
// parent file relative url with file etension
echo $_SERVER['PHP_SELF'] . '<br>';

// parent file name without file extension
echo basename($_SERVER["SCRIPT_FILENAME"], '.php') . '<br>';
// parent file name with file extension
echo basename($_SERVER["SCRIPT_FILENAME"]) . '<br>';
// parent file full url with file extension
echo $_SERVER["SCRIPT_FILENAME"] . '<br>';

//self name without file extension
echo pathinfo(__FILE__, PATHINFO_FILENAME) . '<br>';
//self file extension
echo pathinfo(__FILE__, PATHINFO_EXTENSION) . '<br>';

// parent file name with file extension
echo basename($_SERVER['SCRIPT_NAME']);

Don't forget to remove :)

> <br>

Solution 7 - Php

alex's answer is correct but you could also do this without regular expressions like so:

str_replace(".php", "", basename($_SERVER["SCRIPT_NAME"]));

Solution 8 - Php

you can also use this:

echo $pageName = basename($_SERVER['SCRIPT_NAME']);

Solution 9 - Php

A more general way would be using pathinfo(). Since Version 5.2 it supports PATHINFO_FILENAME.

So

pathinfo(__FILE__,PATHINFO_FILENAME)

will also do what you need.

Solution 10 - Php

$argv[0]

I've found it much simpler to use $argv[0]. The name of the executing script is always the first element in the $argv array. Unlike all other methods suggested in other answers, this method does not require the use of basename() to remove the directory tree. For example:

  • echo __FILE__; returns something like /my/directory/path/my_script.php

  • echo $argv[0]; returns my_script.php\


Update:

@Martin points out that the behavior of $argv[0] changes when running CLI. The information page about $argv on php.net states,

> The first argument $argv[0] is always the name that was used to run the script.

However, a comment from (at the time of this edit) six years ago states,

> Sometimes $argv can be null, such as when "register-argc-argv" is set to false. In some cases I've found the variable is populated correctly when running "php-cli" instead of just "php" from the command line (or cron).

Please note that based on the grammar of the text, I expect the comment author meant to say the variable is populated incorrectly when running "php-cli." I could be putting words in the commenter's mouth, but it seems funny to say that in some cases the function occasionally behaves correctly. 

Solution 11 - Php

Try This

$current_file_name = $_SERVER['PHP_SELF'];
echo $current_file_name;

Solution 12 - Php

This works for me, even when run inside an included PHP file, and you want the filename of the current php file running:

$currentPage= $_SERVER["SCRIPT_NAME"];
$currentPage = substr($currentPage, 1);
echo $currentPage;

Result: > index.php

Solution 13 - Php

Try this

$file = basename($_SERVER['PATH_INFO']);//Filename requested

Solution 14 - Php

Although __FILE__ and $_SERVER are the best approaches but this can be an alternative in some cases:

get_included_files();

It contains the file path where you are calling it from and all other includes.

Solution 15 - Php

Example:

included File: config.php

<?php
  $file_name_one = basename($_SERVER['SCRIPT_FILENAME'], '.php');
  $file_name_two = basename(__FILE__, '.php');
?>

executed File: index.php

<?php
  require('config.php');
  print $file_name_one."<br>\n"; // Result: index
  print $file_name_two."<br>\n"; // Result: config
?>

Solution 16 - Php

$filename = "jquery.js.php";
$ext = pathinfo($filename, PATHINFO_EXTENSION);//will output: php
$file_basename = pathinfo($filename, PATHINFO_FILENAME);//will output: jquery.js

Solution 17 - Php

__FILE__ use examples based on localhost server results:

echo __FILE__;
// C:\LocalServer\www\templates\page.php

echo strrchr( __FILE__ , '\\' );
// \page.php

echo substr( strrchr( __FILE__ , '\\' ), 1);
// page.php

echo basename(__FILE__, '.php');
// page

Solution 18 - Php

As some said basename($_SERVER["SCRIPT_FILENAME"], '.php') and basename( __FILE__, '.php') are good ways to test this.

To me using the second was the solution for some validation instructions I was making

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexView Question on Stackoverflow
Solution 1 - PhpalexView Answer on Stackoverflow
Solution 2 - PhpSparKView Answer on Stackoverflow
Solution 3 - Phpmax4everView Answer on Stackoverflow
Solution 4 - PhpKhandad NiaziView Answer on Stackoverflow
Solution 5 - Phpcharan315View Answer on Stackoverflow
Solution 6 - PhpbegoyanView Answer on Stackoverflow
Solution 7 - PhpuserView Answer on Stackoverflow
Solution 8 - PhpShah AlomView Answer on Stackoverflow
Solution 9 - PhpMegachipView Answer on Stackoverflow
Solution 10 - PhpJoin JBH on CodidactView Answer on Stackoverflow
Solution 11 - PhpWakar Ahmad KhanView Answer on Stackoverflow
Solution 12 - PhpBolliView Answer on Stackoverflow
Solution 13 - PhpAlvin567View Answer on Stackoverflow
Solution 14 - PhpMahdyfoView Answer on Stackoverflow
Solution 15 - PhpEugenView Answer on Stackoverflow
Solution 16 - PhpRahul GuptaView Answer on Stackoverflow
Solution 17 - PhpDariusz SikorskiView Answer on Stackoverflow
Solution 18 - PhpGendrithView Answer on Stackoverflow