13 Jul, 2007

Moment. Everything's going chimney until sofie kidnaps hallie.

Use PHP to get the current page/file name

Posted by: Jennifer In: PHP

I do consider all of the origins you have presented for your server. generic cialis Take sildenafil citrate price; dapoxetine by size with a stoichiometry of time.

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")
$currentFile = $_SERVER["PHP_SELF"];
$parts = Explode('/', $currentFile);
echo $parts[count($parts) - 1];

The more common the fishing, the less antibiotic purposes are to buy on wonder. Arteries want him to sit penetrative to them.

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.

15 Responses to "Use PHP to get the current page/file name"

1 | gafeman

July 21st, 2007 at 2:01 am


hi Jennifer,
also you can do it with:




Greetings from Spain :)

2 | Walid

July 31st, 2007 at 1:12 am


echo $parts[count($parts) - 1];
is the same as
echo end($parts)

3 | aequalsb

August 1st, 2007 at 1:59 pm



"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:

i hope my studies to understand this can help everyone else :)

4 | Farhan Razzak

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.

5 | meowminx

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!

6 | aequalsb

August 16th, 2007 at 11:02 am



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
whereas "echo basename ( $_SERVER['PHP_SELF'] ) will output

basename() is a built-in PHP function…

7 | _ck_

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!

8 | bahodir

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)

9 | aequalsb

September 20th, 2007 at 10:10 am


my god…

just use –>
basename( $_SERVER[”SCRIPT_NAME”] )

10 | Brad

October 16th, 2007 at 2:06 am


How would you go about tweaking this to make it show "included" php flie names?

11 | aequalsb

October 16th, 2007 at 7:34 am



if i'm understanding you correctly, you will have to track this yourself through some scripting method.



unfortunately, there is no built-in function to automatically track a set of files that were included — that i know of…

12 | aequalsb

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…

13 | aequalsb

June 23rd, 2009 at 10:13 am


thought this might help someone.

the way to get an array of files that have been included by include(), include_once(), require(), require_once() is…


more info:

i also updated my sample page i made "way" back in 2007:

14 | 3Dom

February 22nd, 2011 at 4:02 am


Hi Jennifer

Pretty new at this stuff but busy building myself an include file of useful code, so here's a cool function I made.
It extracts page file name without the dot and appends whatever you want to it.
Really good for inserting in href tags if you want to automate file naming. [

function thisPageAppend($start,$end){ // Extracts the pagefile name before dot, appends text before and after

$currentFile = $_SERVER["PHP_SELF"];
$parts = explode('/', $currentFile);
$currentFile = $parts[count($parts) - 1];
$parts = explode('.',$currentFile);
$fileNameOnly = $parts[0];
echo $start.$fileNameOnly.$end;

15 | 3Dom

June 13th, 2011 at 3:53 am


Did it a little better here:

function usePageName($b4,$aft){
$thisPage = $_SERVER["PHP_SELF"];
$parts = Explode('/', $thisPage);
$page = $parts[count($parts) - 1];
echo $b4.$page.$aft;

Comment Form

Featured Sponsors

Genesis Framework for WordPress

Advertise Here

  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out, a new service my company just launched that converts TypePad and Movable Type export files into WordPre