Get File Extension in PHP

This code allows to pass filename in the $file_name variable and function will return file extension only.

<?php
function get_file_extension($file_name)
{
    /* may contain multiple dots */
    $string_parts = explode('.', $file_name); 
    $extension = $string_parts[count($string_parts) - 1];
    $extension = strtolower($extension);
    return $extension;
}
?>

Usage

echo get_file_extension('my_file.php');


Comments

Add your comment