Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to get filetype from a filename?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 261
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: