PHP Get current file name and url

There are many ways to get the current page url or the name of the current file.

How to get the current URL?
To get the current url ( relative to the document root), you may use this script:
<?php
$URL = $_SERVER['PHP_SELF'];
echo $URL;
?>

If your page is www.domain.com/folder1/page.php, this code will display: "/folder1/page.php" .
But if your page is www.domain.com/folder1/page.php?a=25&b=12 , it will display only: "/folder1/page.php" , $_SERVER['PHP_SELF'] don't include the query string .

If you want to include the query string, use this script:
<?php
$URL = $_SERVER['REQUEST_URI'];
echo $URL;
?>

For the page www.domain.com/folder1/page.php?a=25&b=12, this will display /folder1/page.php?a=25&b=12

How to get the file path?
To get the path of the executing file in the server, you may use:
<?php
$path = $_SERVER['SCRIPT_FILENAME'];
echo $path;
?>

display:
C:\www\folder\page.php
or
/var/www/htdocs/test/page.php
How to get the current filename ?
To get the current file name, we will use the same code to get the url:
<?php

$url = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $url);
$file = $break[count($break) - 1];

echo $file;

?>

44 comments:

Anonymous said...

hi
this is very good
but if we want show url after special character
for example If we want show mmm in below URL
xxx.php?mmm

what is this code?


tanks

Jonathan said...

Really nice blog I like it ! ... if you want the best free hosting details at http://hubpages.com/hub/hosting-ms-sql-web and much much more ...

Muhammad Azeem said...

This is a nice article..
Its easy to understand ..
And this article is using to learn something about it..

c#, dot.net, php tutorial, Ms sql server

Thanks a lot..!
0021

Escorts in Mumbai said...

Its really very interesting post full of valuable information. The key part of this post is its descriptive way to define anything.

Buy RS Gold said...


Sure it was an excellent weekend!, Now i'm happy for you!, the clothing is actually georgeous! D3 cd key



Guild Wars 2 Gems

rscredits said...

buy runescape gold with 5% discount code:CHRISTMAS.

Details: http://www.rsorder.com/profile/news/read/id/443

Free 200M runescape gold Giveaway on RSorder Flash Sale! Time: 03:00 AM on December 21, 2012 GMT

To know more: http://www.rsorder.com/profile/news/read/id/441

Like RSorder Facebook to Get 200M Free rs gold! If our facebook fans reach to 10,000, we will send out totally 200M free RS gold.

Rsorder facebook : http://www.facebook.com/RSorderSite?ref=tn_tnmn

Detailed info at: http://www.rsorder.com/profile/news/read/id/435

Fast delivery can be assured if you buy rs gold pay by Western Union at Rsorder!

Guild wars 2 gold said...

This is a great post, i like it very much. I am very much interested in these types of topics from childhood and it's my habit to read this. I'm a editor of game site, you can visit and find diablo 3 gold, Thanks so much for your love to all.

Vehlad said...

thx for the code useful functions keep posting,,,,

saba naaz said...
This comment has been removed by the author.
saba naaz said...

whatsapp dare
whatsapp puzzles

charoli benefits
health and beauty

Rinku Yadav said...

comedy jokes sms
haryanvi jokes
whatsapp dare messages/
whatsapp status
foxtail millet benefits
rice flakes
cool fb status

Rinku Yadav said...

I really appreciate your blog,
you have done the great job.

Health beauty blogs

Dinesh Chauhan said...

Awesome..
Sarkari Result

inshakhan said...

I always like browsing your site.

patiala jokes

Sarkari Nakuri said...

We can get all detail's about Sarkari Result and updates about government jobs.

Sarkari Nakuri said...

All Sarkari Result and updates of government jobs.All upadtes about bank job details as well as exam results.

Sarkari Nakuri said...

Interesting blog with day to day updates Regards Sarkari Result

saba naaz said...

charoli benefits
semolina nutrition
dalia benefits
rice flakes
rock sugar benefits
lassi benefits
amla powder benefits

get rid of bruises
get rid of vaginal odor
get rid of black eye
get rid of toothache
get rid of stomach ache
health beauty blogs
how-to-get-fair-and-glowing-skin

Sarkari Nakuri said...

Sarkari Result and updates about bank job details as well as exam results.

Sarkari Nakuri said...

Latest Sarkari Result and u can get the details about exam updates and results.

Sarkari Nakuri said...

Thanks for the updates Regards Sarkari Result

Sarkari Nakuri said...

Thanks for the information Regards Sarkari Result

K Navatha said...

Good article with excellent idea! I appreciate your post.Regards Sarkari Result

K Navatha said...

That is fantastic information Regards Sarkari Result

K Navatha said...

Good article with excellent idea! I appreciate your post.Regards Sarkari Result

mary Brown said...

I have read your blog its very attractive and impressive. I like it your blog.

PHP Training in Chennai | Online PHP Training

PHP Training in Chennai | Online PHP Training

sindhukk said...

Very interesting information to provide me is possible how to prepare current affairs subject for this Sarkari Result

Anonymous said...

Jokes in Hindi
whatsapp questions
funny jokes in hindi
Hindi Jokes
hindi joke
whatsapp puzzles with answers
puzzles for whatsapp
hindi joks

Rinku Yadav said...

Attitude status in Hindi
Attitude status
Whatsapp Status Ideas
Whatsapp Group Names
Angry Status
Angry Status in Hindi
Very Funny Joke in Hindi
Good Morning Messages
Pet Ki Charbi Kam Karne Ke Upay

Rinku Yadav said...


pregnancy rokne ke upay in hindi
heartbreak quotes
tired quotes
naughty quotes
Relationship Quotes
thought for the day
Quotes

Rinku Yadav said...

hampshire seo consultancy
website designing company hampshire
website development company hampshire
marketing freelancer hampshire
website designing company southhampton
website development company southhampton
SEO Company Hampshire
Divine Gifts

Rinku Yadav said...

pregnancy rokne ke upay in hindi
heartbreak quotes
tired quotes
naughty quotes
Relationship Quotes
thought for the day
Quotes
Status para Whatsapp
Whatsapp Status
symbols status whatsapp
Best whatsapp status
status for whatsapp in punjabi
very funny jokes

Rinku Yadav said...

hampshire seo consultancy
website designing company hampshire
website development company hampshire
marketing freelancer hampshire
website designing company southhampton
website development company southhampton
SEO Company Hampshire
Divine Gifts

Rinku Yadav said...

pregnancy rokne ke upay in hindi
pregnancy rokne ke gharelu upay in hindi
pregnancy ko rokne ka tarika
maths puzzles
pregnancy rokne ke tips hindi
pregnancy se bachne ke upay hindi me
pregnancy me sex karne ka tarika
pregnant nahi hone ke upay in hindi

Rinku Yadav said...

Pimples Ke Liye Gharelu Nuskhe in Hindi
monday morning quotes
Pimples Ke Liye Gharelu Nuskhe
kale daag mitane ke upay in hindi
daag dhabbe hatane ke tips in hindi
math puzzles
maths puzzles
pimple hatane ke tarike hindi me
pimple hatane ke upay in hindi
muhase ka ilaj in hindi
chehre ke daag dhabe door karna in hindi
kil muhase ke upay hindi me
maths puzzles with answers

Rinku Yadav said...

awesome statuses
status for girls
girls status
status for girls in hindi
whatsapp status for girl
cool status for girls
fb status for girls
status for girl
Whatsapp Status For Girls
girly status

Rinku Yadav said...

Whatsapp Dare
Whatsapp Dare Messages
Whatsapp Dare Questions
Saturday Quotes
saturday morning quotes
happy saturday quotes
saturday night quotes
funny saturday quotes
saturday quotes and images
saturday sayings
saturday quotes of the day
saturday images
saturday pictures
saturday images and quotes
Saturday Quotes

Rinku Yadav said...

Jeevan Mantra in Hindi
pet dard ke gharelu nuskhe in hindi
pimple remover tips in hindi
Chehre ke Daag Dhabe Mitane ke Upay
Baal lambe karne ke gharelu nuskhe
gora hone ke upay in hindi
balo ke liye gharelu nuskhe in hindi
mota hone ke upay
matapa kam karne ke upay
Hindi gharelu Nuskhe

Marry Starry said...

I also like Flash, however I am not a good designer to design a Flash, however I have computer software by witch a Flash is automatically created and no more to work. There is no doubt in it that Php is the best way to develop a website, Please write a note on Flash and about its advantages.

Stunning Healthcare Websites for your clinic.

Rinku Yadav said...

best status for whatsapp
whatsapp best status
best whats app status
best love status for whatsapp
best watsapp status
best quotes for whatsapp
best friend status for whatsapp
best whatsapp status

Rinku Yadav said...

Whatsapp Dare
Whatsapp Dare Messages
Whatsapp Dare Questions
Saturday Quotes
saturday morning quotes
happy saturday quotes
saturday night quotes
funny saturday quotes
saturday quotes and images
saturday sayings
saturday quotes of the day
saturday images
saturday pictures
saturday images and quotes
Saturday Quotes

sindhukk said...

Thanking you so much for this great information. You did wonderful work.SarkariResults

kaise kare said...

Thanking you so much for this great information. You did wonderful work.kaise kare

Freshers Job said...

Great Article! i really appreciate this.You are so awesome! This issue has and still is so important and you have addressed it so eloquently.