As part of my occasional series on PHP string manipulation here is a quick PHP function designed to retrieve the file extension from a file path.

function getExt($file){;	
		$ext = substr(strrchr($file,"."), 1);
		return $ext;
}

If you get mixed case file extensions ie ‘image1.jpg’ and ‘image2.JPEG’ then use this variant that will return the file extension in lowercase.

function getExt($file){;	
		$ext = substr(strrchr($file,"."), 1);
		return strtolower($ext);
}

For others in this series try:

One Thought to “Quick PHP Function to Retrieve File Extension”

  1. jf

    here is an easy way

    $extension = pathinfo($file_path, PATHINFO_EXTENSION);

Leave a Comment