To get a filetype from a filename we have define a function GetFiletype() in which we have a passed a parameter i.e.,the name of the file. Using this function we will extract the '.' operator from the filename and use the extension to get filetype.
For example:
Suppose we have a file name info.txt whose filetype we have to find. For this, we will use the following script:
<?php
/* GetFileType */
function GetFiletype($Filename) {
if (substr_count($Filename, ".") == 0) { // Check if there is a dot
return; // Return Nothing
} else if (substr($Filename, -1) == ".") { // Check if the string ends with .
return; // Return Nothing
} else {
$FileType = strrchr ($Filename, "."); // Split the string where the dot is
$FileType = substr($FileType, 1); // Remove the dot
return $FileType; // Return the filetype
}
}
$Filename = "info.txt";
$Filename = GetFileType($Filename);
echo $Filename; // This prints out php4
?>
0 Comment(s)