CancelImage Upload

How to read a filename out of a path variable in PHP

Let's say you have a variable named path, and all that you really require is the name of the file at the end of the path. Through some bizarre combination of programming functions and code structures you have also only been left with the path, and therefore have no way of singularly accessing the files name.

In this situation you can do one of two things. The first, and most obvious choice is to start working on a well defined regular expression that will return to you the value that you are after. The other is the use of one single PHP function.

Indeed, the function basename() is in this instance the far easier, and time sparing option, compared to the time consuming activity of putting together a regular expression. Here is an example of the function basename() in action:


$path = "images/thumbs/image.jpg";

echo basename($path);


As you see there is nothing to it! Never fails to amaze how many predefined functions are available in PHP that could save us programmers so much time, if only we knew about them.

Want to leave a comment?

No problem. Just enter your email and password below.

register | home | reminder

myDesignTool Networking •