PHP Function To Get A File Extension From A String

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);
}

Comments

  1. Phil Thompson

    This looks really useful, thanks.

  2. ochronus

    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. Isaac Van Name

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

  4. Isaac Van Name

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

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

  5. Arup Malakar

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

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

  6. crivion

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

  7. Rafieldex

    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

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

    Just use the pathinfo().

    • rafieldex

      Oh come on…i wrote this post quickly.

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

  9. Isaac Van Name

    @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. Gabriel

    Thanks for sharing !

  11. sumit

    @crivion: please explain it with example

  12. sumit

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

  13. DaveDisaster

    I’ve run a bunch of microtime() tests, and almost every time pathinfo() is the fastest, by a healthy margin over end/explode. substr/strrchr seems to vary somewhere in the middle.

  14. beauty

    There are many way to get file extension. For me, I use
    end(explode('.', $_FILES['fileupload']['name']));
    I think this is easy to understand code from it.

  15. wael fareed

    // gif image uploaded
    if (isset($_FILES['file'])) {
    $file_extension =$_FILES['file']['extension'] ;
    $extension = substr(“$file_extension”, 6); // remove the image/ and the gif extension remains.
    echo $extension // return gif
    }

  16. GotiBandhu

    Hello Everyone,
    In PHP, two function are available $_Get () and $_Post () for passing values from one page to another page, this functions are used to get the values which are passed from a user-filled form (User input value form), like user registration or login form etc…………………… for more details please check out the following link….
    http://www.mindstick.com/Articles/7591b2ed-8eb3-4382-8e72-cd58767a7f29/?PHP $_Get Function

    Thanks !!!!

  17. sudhir

    Thanks , IT is really very user full.


Be Heard!

Share your thoughts without being a jerk! And wrap your code in <code> tags, f00!

Name*:
Email*:
Website: