Using PHP, you might need to strip the path from a URL, leaving behind just the filename at the end of the URL. You can achieve this with a regular expression pattern of course, but I have a much simpler solution. See the example code below.
$url = "http://www.domain.com/path/to/file/filename.php";
$filename = basename($url);
echo $filename; //filename.php
$filename = basename($url);
echo $filename; //filename.php
And that’s all there is to it. I hope that you found this useful.








October 28th, 2009 at 2:26 am
thank u, very useful and classic
March 30th, 2010 at 7:38 pm
that doesn’t work if you have url variables.
April 2nd, 2010 at 6:24 am
Thank you. Such functions are really nice. I’d need a piece of garbage to do that with Javascript.
September 8th, 2010 at 1:22 pm
Great news, I want thank to admin coz’ i have found a lot useful info. Keep it going!
. Best wishes
September 17th, 2010 at 6:40 pm
Hello,
I am looking for a program to strip the url from images so it leaves just the image name. Would this work for that purpose?
October 23rd, 2010 at 12:06 pm
awesome, it works for getting images from urls as well,
i thought basename only worked for paths
May 27th, 2011 at 1:22 pm
@plr store: basename will work on any string – files, folders, or just random junk – it doesn’t actually understand anything about the filesystem, it’s just looking for slashes.
August 3rd, 2011 at 9:14 am
Cool, but it’s not working if you have some parameters in the URL!!
August 3rd, 2011 at 9:37 am
OK, I tried this:
$filename = basename($url);
$parts=explode(“?”,$filename);
$filename = $parts[0];
echo $filename;
And it works!!
October 28th, 2011 at 2:03 am
thanks a lot
December 21st, 2011 at 5:53 am
Thank you!
Brilliant and simple.