Home >Backend Development >PHP Tutorial >A php function to get the file extension_PHP tutorial
/*
GetFileType
Usage: GetFiletype($filename)
*/
function GetFiletype($Filename) {
if (substr_count($Filename, ".") == 0) { // Check whether there is a . sign in the file name.
return; // Return empty
} else if (substr($Filename, -1) == ".") { // Check whether it ends with ., that is, no extension
return; // Return empty
} else {
$FileType = strrchr ($Filename, "."); // Cut from . number
$FileType = substr ($FileType, 1); // Remove . number
return $FileType; // Return
}
}
$Filename = "Testfilename.php4";
$Filename = GetFileType($Filename);
echo $Filename; // Print out php4
?>