Page 1 of 1
Current Filename?
Posted: Sat Nov 19, 2005 5:11 pm
by po
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
Posted: Sat Nov 19, 2005 5:27 pm
by Chris Corbyn
Code: Select all
<?php
$file = basename(__FILE__);
preg_match('/^(.*?)\.[^\.]*$/', $file, $matches);
echo $matches[1];
?>
Posted: Sat Nov 19, 2005 5:44 pm
by po
Sweet, thanks.
Posted: Sat Nov 19, 2005 5:51 pm
by AKA Panama Jack
Actually you really should use this if you want to get the name of the php program currently being executed.
Posted: Sat Nov 19, 2005 5:57 pm
by lilleman
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, '.'));
Posted: Sat Nov 19, 2005 6:01 pm
by Jenk
$_SERVER['PHP_SELF'] is tainted, long discussion well worth reading here:
viewtopic.php?t=39853
as d11wtq replied, or:
Code: Select all
<?php
$filename = substr(basename(__FILE__), 0, strrpos(basename(__FILE__), '.'));
?>
Posted: Sat Nov 19, 2005 6:03 pm
by Jenk
lilleman 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, '.'));
That doesn't remove the extension
This works:
Code: Select all
$name = substr($filename = basename(__FILE__), 0, strrpos($filename, '.'));
Posted: Sat Nov 19, 2005 6:08 pm
by lilleman
That's what happens when you're too lazy to test the code, because you're sure it'll work...
