Reputation: 68072
If I have PHP script, how can I get the filename of the currently executed file without its extension?
Given the name of a script of the form "jquery.js.php", how can I extract just the "jquery.js" part?
Upvotes: 330
Views: 502354
Reputation: 335
Try this
$file = basename($_SERVER['PATH_INFO']);//Filename requested
Upvotes: 2
Reputation: 1958
$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. 😁
Upvotes: 5
Reputation: 1386
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
?>
Upvotes: 4
Reputation: 1173
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.
Upvotes: 3
Reputation: 517
Try This
$current_file_name = $_SERVER['PHP_SELF'];
echo $current_file_name;
Upvotes: 3
Reputation: 12140
See http://php.net/manual/en/function.pathinfo.php
pathinfo(__FILE__, PATHINFO_FILENAME);
Upvotes: 75
Reputation: 5274
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
Upvotes: 3
Reputation: 316
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
Upvotes: 1
Reputation: 5211
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.
Upvotes: 150
Reputation: 490597
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, '.'));
}
Upvotes: 490
Reputation: 424
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>
Upvotes: 29
Reputation: 4407
__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
Upvotes: 1
Reputation: 2396
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.
Upvotes: 65
Reputation: 389
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.
Upvotes: 5
Reputation: 10161
$filename = "jquery.js.php";
$ext = pathinfo($filename, PATHINFO_EXTENSION);//will output: php
$file_basename = pathinfo($filename, PATHINFO_FILENAME);//will output: jquery.js
Upvotes: 1
Reputation: 1055
you can also use this:
echo $pageName = basename($_SERVER['SCRIPT_NAME']);
Upvotes: 9
Reputation: 301
This might help:
basename($_SERVER['PHP_SELF'])
it will work even if you are using include.
Upvotes: 30
Reputation: 16839
alex's answer is correct but you could also do this without regular expressions like so:
str_replace(".php", "", basename($_SERVER["SCRIPT_NAME"]));
Upvotes: 22