Current Filename?
Moderator: General Moderators
Current Filename?
Hi, I'm wondering how I can get the filename (without the extension and URL) of the current PHP file?
For example, if myfile.php is loaded, I want to somehow get the filename myfile in that page. I've taken a look at the basename() function, but I couldn't really figure out how to use it to get the current file.
-Thanks
For example, if myfile.php is loaded, I want to somehow get the filename myfile in that page. I've taken a look at the basename() function, but I couldn't really figure out how to use it to get the current file.
-Thanks
- Chris Corbyn
- Breakbeat Nuttzer
- Posts: 13098
- Joined: Wed Mar 24, 2004 7:57 am
- Location: Melbourne, Australia
Code: Select all
<?php
$file = basename(__FILE__);
preg_match('/^(.*?)\.[^\.]*$/', $file, $matches);
echo $matches[1];
?>- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
Actually you really should use this if you want to get the name of the php program currently being executed.
Code: Select all
$_SERVER['PHP_SELF']In this situation, I'd prefer to use the string functions instead of a regular expression:
Code: Select all
$name = substr(basename($filename = __FILE__), 0, strrpos($filename, '.'));$_SERVER['PHP_SELF'] is tainted, long discussion well worth reading here: viewtopic.php?t=39853
as d11wtq replied, or:
as d11wtq replied, or:
Code: Select all
<?php
$filename = substr(basename(__FILE__), 0, strrpos(basename(__FILE__), '.'));
?>
Last edited by Jenk on Sat Nov 19, 2005 6:09 pm, edited 1 time in total.
That doesn't remove the extensionlilleman wrote:In this situation, I'd prefer to use the string functions instead of a regular expression:
Code: Select all
$name = substr(basename($filename = __FILE__), 0, strrpos($filename, '.'));
This works:
Code: Select all
$name = substr($filename = basename(__FILE__), 0, strrpos($filename, '.'));