IT & Programming

Get file extension with PHP

  • How to get file extension with PHP.
  • Custom PHP function to get file extension.

Function accepts a string parameter which is the name of complete file name. It returns the extension against supplied file name.

Function

function getExtension($string){

    $i = strrpos($string,'.');

    if (!$i) {
        return '';
    }

    $l = strlen($string) - $i;
    $extension = substr($string,$i+1,$l);
    return $extension;
}

Calling The Function

echo getExtension('image.jpg');

Output

.jpg

Leave A comment

Email address is optional and will not be published. Only add email address if you want a reply from blog author.
Please fill required fields marked with *