scriptygoddess

13 Jul, 2007

Use PHP to get the current page/file name

Posted by: Jennifer In: PHP

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.

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

1 | gafeman

July 21st, 2007 at 2:01 am

Avatar

hi Jennifer,
also you can do it with:

basename($_SERVER['PHP_SELF'])

and

basename(__FILE__)

Greetings from Spain :)

2 | Walid

July 31st, 2007 at 1:12 am

Avatar

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

3 | aequalsb

August 1st, 2007 at 1:59 pm

Avatar

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

4 | Farhan Razzak

August 16th, 2007 at 12:41 am

Avatar

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

Avatar

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

Avatar

@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…

7 | _ck_

August 19th, 2007 at 7:06 am

Avatar

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

Avatar

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

Avatar

my god…

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

10 | Brad

October 16th, 2007 at 2:06 am

Avatar

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

11 | aequalsb

October 16th, 2007 at 7:34 am

Avatar

@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…

12 | aequalsb

October 16th, 2007 at 7:36 am

Avatar

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

Avatar

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…

get_included_files()

more info:
http://us2.php.net/manual/en/function.get-included-files.php

i also updated my sample page i made "way" back in 2007:
http://abdstudios.net/examples/dirname-basename/

14 | 3Dom

February 22nd, 2011 at 4:02 am

Avatar

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

Avatar

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;
};

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 tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements