Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

PHP Function To Get A File Extension From A String

14 Responses »

Getting the file extension from a PHP string can be very important in validating a file or file upload. I've written a simple PHP function to retrieve a file extension from a string.

The Code

function get_file_extension($file_name)
{
	return substr(strrchr($file_name,'.'),1);
}

Discussion

  1. January 20, 2008 @ 12:09 pm

    This looks really useful, thanks.

  2. October 17, 2008 @ 8:23 am

    Why reinvent the wheel? We have good ol’ pathinfo() :
    http://hu2.php.net/manual/en/function.pathinfo.php

    Array
    (
    [dirname] => .
    [basename] => some.file.extension
    [extension] => extension
    [filename] => some.file
    )

  3. February 27, 2009 @ 3:59 pm

    I came up with this snippet to get the file extension as well, using array functions:

  4. February 27, 2009 @ 4:00 pm

    … and it got cut out. Here’s the function, without tags:

    function get_file_extension($file_name) {
    return end(explode(‘.’,$file_name));
    }

  5. March 6, 2009 @ 2:20 am

    As mentioned by ochronus the following code does the exact same thing:

    $file = ‘/tmp/test.php’;
    echo pathinfo($file, PATHINFO_EXTENSION);

  6. March 23, 2009 @ 3:42 am

    or we can do it via end(explode(‘.’, “filename.ext)); easily

  7. rafieldex
    May 13, 2009 @ 2:34 pm

    File extension from path:

    function f_extension($fn){
    $str=explode(‘/’,$fn);
    $len=count($str);
    $str2=explode(‘.’,$str[($len-1)]);
    $len2=count($str2);
    $ext=$str2[($len2-1)];
    return $ext;
    }

    $extesnion=f_extension(“files/file.jpg”); // returns jpg

    Try & enjoy!

  8. jorge
    January 7, 2010 @ 6:12 am

    What happens when you try to get the extension of “/etc/init.d/README” with the explode method?

    Just use the pathinfo().

  9. January 7, 2010 @ 9:51 am

    @Jorge: Either way works, and it just ends up being a matter of preference. You have to consider how this function will usually be used on a web application; it will be grabbing the extension and making sure it’s an allowed extension for upload. Thus, the explode method would return something that doesn’t match a valid extension (like your README example), and the file upload would get declined.

    If you’re building a file explorer on the web, sure, use pathinfo(); that’s common sense. If you’re building a web app that takes uploads, then I can’t imagine the explode method would not be faster than pathinfo.

  10. March 2, 2010 @ 2:23 pm

    Thanks for sharing !

  11. sumit
    April 27, 2010 @ 8:17 am

    @crivion: please explain it with example

  12. sumit
    April 27, 2010 @ 8:19 am

    end(explode(‘.’, “filename.ext));,
    explain how does it work

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!