Extract a file extension and check if the extension is allowed (using PHP)

Going to start work on a logo sharing portal. Yes, sharing, not selling. 🙂 It is quite a pain when you need to find a vector logo for an existing company, and all you get is some low resolution picture. Add to it that most of the people you can contact have no idea what are vector graphics and are proposing that you “Scan it off the business card.”.. So I needed functions for extension extraction and allowance check.. Nothing spectacular..

//Gets lowercase extension for the given file
function getExtension($file) {
	$extension = substr(strrchr($file, '.'), 1);
	return strtolower($extension);
} 

//Checks if the given file extension is in the allowed extensions
function checkFileType($file, $allowedExtensions) {
	$extension = getExtension($file);
	if (in_array($extension, $allowedExtensions)) {
	    return true;
	}
	return false;
}

//Test..
$allowed = array("jpg", "bmp", "png", "svg");
$file = "/home/root/images/picture1.bmp";

//Returns true (1)
echo checkFileType($file, $allowed);
Tagged , ,

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.