View Full Version : Get A File's Name

03-19-2003, 11:00 PM
Say I have a file located at www.site.com/folder/file.php. I would like file.php to detect it's own file name (as the same script is used in multiple files).

$_SERVER['PHP_SELF'] will return folder/file.php

I really want to get the file name only (minus the folder(s) or the extension).

Anyone know if this is this possible?

03-20-2003, 12:33 PM
I think you might need to substr the return from $_SERVER['PHP_SELF'] to get just the script name.

$the_lot = $_SERVER['PHP_SELF'];
$the_script = substr($the_lot,strrpos($the_lot,'/'));

if you wanted just 'file' rather than file.php - maybe

$the_script = substr($the_lot,strrpos($the_lot,'/'),strrpos($the_lot,'.')-strlen($the_lot));

might need a +1 or -1 here or there.

03-20-2003, 10:49 PM


to get only the file name + extension, and

preg_replace('/(.+)(\..+?)$/', '\\1', basename($_SERVER['PHP_SELF']));

to get only the filename without the extension. But beware - a file is not required to have a file extension, and the .something part may be irrelevant to the file's purpose.