How to get a file's extension from the filename
This function gets the extension of a provided filename. It is useful in upload scripts in order to permit the upload of only certain types of files.
function get_extension ($filename) {
// This function reads the extension of a file. It is used in
// the image uploading process.
// Find the position of the LAST dot in the filename.
$dot_position = strrpos($filename,".");
// Check to see if we got results above, if not, return nothing,
// ending the function.
if (!$dot_position) {
return "";
}
// If a dot was found, the function continues.
// Set $length to the length of the filename minus the position
// of the dot.
$length = strlen($filename) - $dot_position;
// Set $ext to
$extension = substr($filename, $dot_position + 1, $length);
// Make sure it's in lowercase
$extension = strtolower($extension);
return $extension;
}
