Getting filename and extension in PHP using explode() ,basename() and pathinfo()

Getting the extension from a file name or name of the file from the full path is not a big hassle for the experienced programmer but it might be a annoying task for the beginners of PHP. Now let me find the solution for them using two functions of PHP called explode() and pathinfo().

Getting extension of the file using explode() function

function getFileExtension($fileName)
   return $parts[count($parts)-1];

As you can see in the first line of the function, the string is separated with dot(.) character and the last array is returned as the extension of the filename.

Now let’s look at the better approach of finding extension even filename from the path using a useful function called pathinfo()

Getting file name using basename() function

echo basename('/var/website/htdocs/test.php'); //output test.php
echo basename('/var/website/htdocs/test.php','.php'); //output test

The first line outputs test.php in the browser. The basename() function returns the file name component of the path. And in the second the statement the suffix is supplied, if suffix is supplied as the second argument then it will cut off suffix from the file name.

Getting extension of the file using pathinfo() function

Pathinfo() is a very useful function for getting extension, filenames well as the directory name.This functions returns the path information in a array with the following elements dirname, basename and extension. Let’s look at the example of pathinfo() function first

 $parts = pathinfo('/var/website/htdocs/index.php');
 echo $parts ['basename'], "<br />";
 echo $parts ['extension'], "<br />";
 echo $parts ['dirname'], "<br />";

Let output of the above will be

index.php – this is file name
php – this is extension
/var/website/htdocs/ – and this is directory name

Now i don’t have to explain about the following function which returns file extension with getFileExtension() from a path or filename and name of the file with getFileName() from a path.

//function to return file extension from a path or file name
function getFileExtension($path)
  return $parts['extension'];
//function to return file name from a path
function getFileName($path)
  return $parts['basename'];

8 thoughts on “Getting filename and extension in PHP using explode() ,basename() and pathinfo()

  1. will

    your method is neat, but i don’t understand why you didn’t use php’s built-in function … basename();

    it will automatically get the filename found at the end of a path, and all you would have to do is retrieve a substring from the . to the length of the filename….

  2. ya will you are absolutely right.I agree with you.
    sorry that i’ve forgotten to mention about that function, I will sure update the post thanks a lot for reminding..

  3. Chetan

    this trcks are working but i have one problem
    that, i am stored uploaded path in db like “/photo/Thumb/f/image.jpg”. When will search images form name like “image” then it will give proper output but i will put “.jpg” or “*.jpg” it can’t work. So what should i do? please help and guide me
    to overcome from that problem.


    Chetan Chopkar

  4. Are you searching using database query then you can use “%” wild card character

    where images like “image%”

    hope this helps..

  5. Bamwamba

    Hi Roshan, this is a brilliant post. Got a small problem. What if the filename is unknown for instance, if all you have is the name or rather the directory path? Using your example, $parts = pathinfo(‘/var/website/htdocs/index.php’); Lets imagine we didn’t know the name of the file (index.php) but you have the directory (/var/website/htdocs/) how would you access it?

    Kind Regards,

  6. Hardik

    this is really very usefull for me and save a lot time of mine keep going.

Leave a Reply

Your email address will not be published. Required fields are marked *