Getting a file’s extension is essential when working with uploads and other manipulations complying with files. You might have a user uploading a file and you need to rename the file before moving it from the temporary upload directory. In order to do so, you can obtain the file extension and then rename it to whatever you like. Below is a custom function I wrote which will help you to get the extension of any file using PHP.

function stripExtension($filename = '') {
    if (!empty($filename)) {
        $filename = strtolower($filename); 
        $extArray = split("[/\\.]", $filename); 
        $p = count($extArray) - 1; 
        $extension = $extArray[$p]; 
        return $extension;
    } else {
        return false;

So basically, we split the name of the file on every dot (.). The PHP split() function creates an array of strings for us. We then take the string obtained after the last dot (.). Even though there might be more than one period within the filename, we know that the last string will be the extension.

Now, with the full file extension in your possession, you can continue and use it for something specific.

I hope that you found this useful!