[TIPS] Get File Extension Easily in PHP

Tuesday May 5, 2009 by  

PHP Logoi 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…

What people search:

Other Interesting Articles:

Post comment as twitter logo facebook logo
Sort: Newest | Oldest

this is great!. Thanks a lot for sharing.

And don't forget about:

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

And don't forget about:

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

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

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

Hi, nice posts there :-) thank's concerning the interesting advice

Hi, nice posts there :-) thank's concerning the interesting advice

Great tips! Thanks for sharing..

Great tips! Thanks for sharing..