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) );
}

 

Posted by Jogesh Sharma

Jogesh Sharma is a web developer and blogger who loves all the things design and the technology, He love all the things having to do with PHP, WordPress, Joomla, Magento, Durpal, Codeigniter, jQuery, HTML5 etc. He is the author of this blog.