Use PHP to get the current page/file name
I probably already have this posted somewhere, I know I use it a ton but always have to look it up for the exact syntax. Here’s how you can get the name of the current file (ie. if your file is “aboutus.php” this will echo “aboutus.php”)
<?php
$currentFile = $_SERVER["PHP_SELF"];
$parts = Explode(’/', $currentFile);
echo $parts[count($parts) - 1];
?>
I’ve seen variations on it, as I said, I always have to look it up for the exact syntax - but today I found that from here. Also here - which seems to list a bunch of other useful snippets.
July 21st, 2007 at 2:01 am
hi Jennifer,
also you can do it with:
basename($_SERVER['PHP_SELF'])
and
basename(__FILE__)
Greetings from Spain
July 31st, 2007 at 1:12 am
echo $parts[count($parts) - 1];
is the same as
echo end($parts)
August 1st, 2007 at 1:59 pm
actually…
“echo basename( $_SERVER['PHP_SELF'] )” and “echo basename( __FILE__ )” can return the same results or entirely different results.
why? __FILE__ always refers to the file ITSELF — whether it is included or not whereas PHP_SELF always refers to the “parent” file (the one doing the including).
you can memorize it like this –> (P)HP_SELF = (P)arent and __FILE__ = __child
for more details, check this URL where i’ve set up an example:
http://abdstudios.net/examples/dirname-basename/
i hope my studies to understand this can help everyone else
August 16th, 2007 at 12:41 am
Good example Mr.aequalsb
u have clearly explain both things ,
but $_SERVER["PHP-SELF"] gives the absolute path not the exact file name.
August 16th, 2007 at 9:49 am
Hi! I wanted to leave this comment in the htaccess bit, but I couldn’t find the comment form. I just want to thank you for the piece of code in the htaccess you have! I finally got it working without throwing a 500 internal server error! My friend has been puzzled as to why the code she gave me kept doing that! Thank you so much!
August 16th, 2007 at 11:02 am
@Farhan
that’s why you use the basename function (as clearly inidcated in the example)
basename( $_SERVER['PHP_SELF'] )
so “echo $_SERVER['PHP_SELF'] will output something like
/www/htdocs/index.php
whereas “echo basename ( $_SERVER['PHP_SELF'] ) will output
index.php
basename() is a built-in PHP function…
August 19th, 2007 at 7:06 am
If you dig around the wordpress/bbpress code you’ll see that PHP_SELF doesn’t always work as wanted on Windows servers with PHP, etc. They have a function that tries it three different ways!
September 19th, 2007 at 12:42 am
filename without path can be found by this way:
substr($_SERVER["SCRIPT_NAME"], strrpos($_SERVER["SCRIPT_NAME"],”/”) + 1)
September 20th, 2007 at 10:10 am
my god…
just use –>
basename( $_SERVER[â€SCRIPT_NAMEâ€] )
October 16th, 2007 at 2:06 am
How would you go about tweaking this to make it show “included” php flie names?
October 16th, 2007 at 7:34 am
@Brad
if i’m understanding you correctly, you will have to track this yourself through some scripting method.
example:
========================
========================
unfortunately, there is no built-in function to automatically track a set of files that were included — that i know of…
October 16th, 2007 at 7:36 am
oops… in the post above, the PHP code wasn’t translated automatically into HTML entities and looks like it may have been removed…
maybe scriptygoddess can check the post and fix it. otherwise, i no longer have the time to resubmit my post…