Get the file extension of a file using PHP

Using PHP, getting the file extension (i.e. the “.jpg” part of the file name) of a file pretty easy:

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

This is useful for instances where you need to create an image tag in your HTML and you don’t know what the file extension is going to be, or if different file type require different attributes etc.

About Joseph R. B. Taylor

Joseph R. B. Taylor is a humble designer/developer who makes stuff for screens of all shapes and sizes. He is currently the lead UI/UX Architect at MScience, LLC, where he works to create simple experiences on top of large rich datasets for their customers and clients.