Get file extension with php

When we give permission to users to upload a file, then it is must to validate that file. And that validation is to get the extension of the file and check if the file extension is match with the related extesions or not. Here is a small function to get the file extesion with php.


This function return the extension with ‘.extension_name’:

function getExtension( $filename ){
    return strrchr($filename, '.');
}

 

And if you want the extension without then simply do something like:

function getExtension( $filename ){
    $ext = strrchr($filename, '.');
    return substr( $ext, 1, strlen($ext) );
}