PHP Tutorial : Get file extension

With the next three lines of php code you will be able to get a file extension.

<?php
$file = "somefile.gif";
$ext = end(explode('.', $file));

print "Your file extension is $ext";
?>

This entry was posted in Short PHP Functions and tagged , , , , , . Bookmark the permalink.

6 Responses to PHP Tutorial : Get file extension

  1. Jelle says:

    This works very well.

    Can be used to check files in a download script and reject certain file-types.

  2. admin says:

    Yup I agree! Thanks

  3. Juan says:

    what if there s a file called whatever.whatever.mp3?

  4. admin says:

    What would be the problem? no problem it will work with any file extension

  5. Maitane says:

    commenting usually isnt my thing, but ive spent an hour on the site, so thanks for the info

  6. admin says:

    cool, thank you very much! subscribe to feed too I keep it updated – at least I try.

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>