[TIPS] Get File Extension Easily in PHP

I want to share a tips for you to get file extension in PHP easily. Before i always use the old way to get the file extension, that is using substring and find the last “.” (dot), and get the extension. For those who still doing this, i have a tips for you to get the extension easier.

You can use pathinfo, with pathinfo you can get information about a file path, here is the example how to use it:

<?php
$fileinfo = pathinfo("/path/to/your/file.php");
echo $fileinfo['dirname']; // Get directory
echo $fileinfo['basename']; // Get file basename
echo $fileinfo['extension']; // Get file extension
echo $fileinfo['filename']; // Get file name
?>

That’s it! Thank you for visiting my blog and have a nice day…

Comments

  1. Leoslab says:

    Great tips! Thanks for sharing..

  2. Leoslab says:

    Great tips! Thanks for sharing..

  3. Baraninc says:

    Hi, nice posts there 🙂 thank's concerning the interesting advice

  4. Baraninc says:

    Hi, nice posts there 🙂 thank’s concerning the interesting advice

  5. crivion says:

    what about
    $filename = "f.i.l.e.ext";
    print end(explode(".", $filename));

  6. crivion says:

    what about
    $filename = “f.i.l.e.ext”;
    print end(explode(“.”, $filename));

  7. And don't forget about:

    // code
    $ext = pathinfo(__FILE__, PATHINFO_EXTENSION);
    print $ext;
    // end code

  8. And don’t forget about:

    // code
    $ext = pathinfo(__FILE__, PATHINFO_EXTENSION);
    print $ext;
    // end code

  9. Malini Jay says:

    this is great!. Thanks a lot for sharing.

  10. Thanks A Lot.

Give me your feedback

This site uses Akismet to reduce spam. Learn how your comment data is processed.