Categorized | News

Getting a file extension

Posted on 18 May 2002 by Demian Turner

Getting a file extension is simple with this code snippet:

$ext = substr($str,strrpos($str,’.’));

Bookmark and Share

7 Comments For This Post

  1. Anonymous Says:

    $str = \’filename.mp3\’;
    $ext = strrchr($str,\’.\’);
    the difference is that the extension is returned with the dot in the head but it is as easy to check against \’.mp3\’ as against \’mp3\’ or any other file format.

  2. Anonymous Says:

    a combination of the two gets the extension without the dot:

    $ext = substr(strrchr($filename, \’.\’), 1);

  3. Anonymous Says:


  4. demian Says:

    definitely shorter – nice one 😉

  5. Anonymous Says:

    do you consider file name like
    or 4 letter extensions like .jpeg or .mpeg
    php has a ready solution : the pathinfo() function (since v. 4.03)
    even if you use explode you must do something like :
    $fname = \’\’;
    $exts = explode(\’.\’, $fname);
    print $exts[sizeof($exts)-1];

  6. Anonymous Says:

    check out the suggestion from September 29 2002, an even shorter, more elegant way 😉

  7. ninware Says:

    nice one short and simple

Leave a Reply



Demian Turner's currently-reading book recommendations, reviews, favorite quotes, book clubs, book trivia, book lists



PHPkitchen recommends you also check out the following sites :

Accounting for Small Businesses

FreeAgent sign-up