PHP Get File Extension
I see a lot of people providing examples of getting the file extension of a file in PHP using string manipulation techniques, splitting on the dot, etc and just wanted to share the way that I do it. Basically the php pathinfo() function will return the extension as one of its parameters, or only the extension if you specify the optional second parameter as PATHINFO_EXTENSION. I use the php basename() function as well so that I can use the same code snippet when I want to pass in a filename that contains a path, instead of first pulling out the path.
function get_file_extension($filename){ return pathinfo(basename($filename),PATHINFO_EXTENSION); }
So a few simple usage examples and this post is done
echo get_file_extension('functions.php'); // php echo get_file_extension('/path/functions.php'); // php
About this entry
You’re currently reading “PHP Get File Extension,” an entry on PHP Functionalism
- Published:
- 11.16.11 / 9am
- Category:
- Tips & Tricks
- Tags:
No comments
Jump to comment form | comments rss [?] | trackback uri [?]