Page 1 of 1

Easiest way to fetch filename

Posted: Tue Mar 06, 2007 8:13 pm
by abeall
Strangely I can't seem to find an easy way to determine the filename of the currently running script. I don't want the pathname, just the filename itself. Example, for a file in http://www.foo.com/bar/blah.php, I would want blah.php.

Basically, I have a series of php files which draw together multiple other files, one of which(the main body text) will be according to the current script filename. Example:

/pages/mypage.php
/pages/mypage.html
/inc/layout.html

What I want to do is use mypage.php to grab layout.html, do a str_replace of a <content/> tag using the contents of mypage.html

Thanks.

Posted: Tue Mar 06, 2007 8:56 pm
by abeall
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Here's what I'm doing:

Code: Select all

echo substr(array_pop(explode('/',$_SERVER['PHP_SELF'])),'',-4);

// or, broken apart:

$pagepath = $_SERVER['PHP_SELF']);
$pagepath_arr = explode('/',$pagepath);
$pagename = array_pop($pagepath_arr);
$filename = substr($pagename,'',-4);
Seems to work.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Mar 06, 2007 9:24 pm
by hawleyjr

Code: Select all

echo basename( __FILE__ );

Posted: Tue Mar 06, 2007 9:30 pm
by abeall
basename()

Perfect. Thanks.

Posted: Wed Mar 07, 2007 12:03 am
by RobertGonzalez
When using the __FILE__ constant, make sure to call it from the file you are in otherwise you will get the name of the file that the __FILE__ constant is actually in (on some systems). Another alternative is to use basename($_SERVER['SCRIPT_FILENAME']) or basename($_SERVER['PHP_SELF']).