Home > PHP Strings > Find File Extension In PHP

Find File Extension In PHP

February 23rd, 2009 Leave a comment Go to comments

This simple code example uses a combination of strrchr to find the last occurrence of a string and substr to return part of the string in order to find the file extension for a given filename. This is ideal if you want to quickly find a file extension.

$ext = substr(strrchr($fileName, '.'), 1);

This code can be used in the following way.

$fileName = '\path\to\file\afile.wibble';
$ext = substr(strrchr($fileName, '.'), 1);
echo $ext;

The output here is 'wibble';

  1. ruben
    February 9th, 2010 at 11:04 | #1

    $extension = pathinfo($path, PATHINFO_EXTENSION)

  1. No trackbacks yet.