How to get the file extension in PHP?

Keith Power picture Keith Power · Apr 29, 2012 · Viewed 635.6k times · Source

I wish to get the file extension of an image I am uploading, but I just get an array back.

$userfile_name = $_FILES['image']['name'];
$userfile_extn = explode(".", strtolower($_FILES['image']['name']));

Is there a way to just get the extension itself?

Answer

ThiefMaster picture ThiefMaster · Apr 29, 2012

No need to use string functions. You can use something that's actually designed for what you want: pathinfo():

$path = $_FILES['image']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);