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;

?>

54 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

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!

Vehlad said...

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

Unknown said...
This comment has been removed by the author.
Unknown said...

whatsapp dare
whatsapp puzzles

charoli benefits
health and beauty

Unknown said...

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

Unknown said...

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

Health beauty blogs

Unknown said...

Awesome..
Sarkari Result

inshakhan said...

I always like browsing your site.

patiala jokes

Unknown said...

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

Unknown said...

Interesting blog with day to day updates Regards Sarkari Result

Unknown 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

Unknown said...

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

Unknown said...

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

Unknown said...

Thanks for the updates Regards Sarkari Result

Unknown said...

That is fantastic information Regards Sarkari Result

sindhukk said...

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

Unknown 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

Unknown said...


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

Unknown 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

Unknown 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

Unknown 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

Unknown 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

Unknown 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

Jimmy Mehr 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.

Unknown 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

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.

Digital Khabar said...



बालों की सुंदरता को बढ़ाने के तरीके



रिंकल्स को कम करने के उपाए



घर पर जल्दी वजन घटाने के उपाय


मुँहासे और चहरे पर दाने हटाने के उपाय


कब्ज़ की समस्या


सीने में जलन के लिए घरेलू उपचार

Unknown said...

tourist places in delhi
relationship advice
gmail sign up kaise kare

ladki ko vash me karne ka mantra
bodybuilding tips in hindi
facebook se video kaise download kare

Unknown said...

This is a nice article..
Its easy to understand ..
rang gora karne ka tarika

Unknown said...

bodybuilding kaise kare
pimple marks kaise hataye
short status for whatsapp in hindi
computer sort key in hindi
ladki patane ka mantra
how to make hair style in hindi
hair growth tips in hindi
mango pickle recipe

Deepika said...

This is a nice article..
Its easy to understand ..short status for whatsapp in hindi

Unknown said...


the secret law of attraction
Crack heel repair in hindi
Ladki ko impress karne ke tarike

daily makeup kaise kare
Homemade tips for pink lips in hindi
Makeup tips in hindi

Unknown said...

ladki patane ka mantra
Study Tips In Hindi
Golgappa recipe in hindi

Face Makeup Karne Ka Tarika
Happy Dussehra images & wishes in hindi

Unknown said...

green tea kaise banaye
Word Meaning In Hindi
Mutton Biryani Kaise Banaye

face pack for glowing skin
Motapa Kam Karne Ke Upay Baba Ramdev
Best Chowmein Recipe In Hindi

Unknown said...

latest fashion trends for women
Good Morning Image in hindi
Vastu Tips For Home

mobile number kaise track kare
Tenses Chart In hindi
Personality Development Tips In Hindi

Intreends.com - Trending News and Tutorials said...

Thank you for sharing, This is a nice article..
Its easy to understand ..
And this article is using to learn something about it Intreends.com

Templatesme.com said...

Download Website Template, Blogger Template and Powerpoint Template in
Templatesme.com

Sunny said...

Vaastu Tips

Andriod Apps Download said...

Download Mad Rocket Fog Of War
Mad Rocket Fog Of War
Download Mad Rocket Fog Of War
Mad Rocket Fog Of War
Download Mad Rocket Fog Of War
Mad Rocket Fog Of War
Download Mad Rocket Fog Of War
Mad Rocket Fog Of War
Download Mad Rocket Fog Of War
Mad Rocket Fog Of War
Download Mad Rocket Fog Of War
Mad Rocket Fog Of War
Download Mad Rocket Fog Of War
Mad Rocket Fog Of War
Download Mad Rocket Fog Of War
Mad Rocket Fog Of War
Download Mad Rocket Fog Of War
Mad Rocket Fog Of War
Download Mad Rocket Fog Of War
Mad Rocket Fog Of War
Download Mad Rocket Fog Of War
Mad Rocket Fog Of War
Download Mad Rocket Fog Of War
Mad Rocket Fog Of War


Saro said...

I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 
rpa training in bangalore
best rpa training in bangalore
RPA training in bangalore
rpa course in bangalore
rpa training in chennai
rpa online training

ragul ragul said...

This is quite educational arrange. It has famous breeding about what I rarity to vouch. Colossal proverb. This trumpet is a famous tone to nab to troths. Congratulations on a career well achieved. This arrange is synchronous s informative impolites festivity to pity. I appreciated what you ok extremely here 
AWS Training in pune
AWS Online Training

Anjali M said...
This comment has been removed by the author.
Harini Priya said...
This comment has been removed by the author.
Punjabi Status 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

Reshma said...

Thanks for this wonderful blog it is really informative to all.keep update more information about this...
Tally Course in Bangalore
Tally Training in Bangalore

Hussey said...

Extraordinary Blog. Provides necessary information.
java training center in chennai
​​best java coaching centre in chennai

sam said...

Thanks for this wonderful blog it is really informative to all.keep update more information about this...

PTE institute in ambala
Best IELTS Institute in Ambala
Best IELTS Coaching in Ambala

Pavithra Devi said...

This post is so interactive and informative.keep update more information...
Ethical Hacking Course in Tambaram
Ethical Hacking Course in Chennai

Matt Reeves said...

Mindblowing blog very useful thanks
DevOps Training in T Nagar
DevOps Training in Chennai