PHP Code

File Extension Using PHP

This post explains about how to get file extenstion using the php. In the we have two types to retrieve the file extensions.

How to get file extension using PHP by Anil Kumar Panigrahi
How to get file extension using PHP by Anil Kumar Panigrahi

See the  following script :

Type 1:

<?php
function findextension ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
$filename="image.jpg";
echo findextension ($filename);
// Here we will get the file extension

$filename="image.jpg";

echo findextension ($filename);

// Here we will get the file extenstion

?>

Type 2:

Another way to get file extension:

<?php

function file_extension($filename)
{
return end(explode(".", $filename));
}

echo file_extension("anil.labs.jpg");
?>

Thanks,
Anil Kumar Panigrahi

4 thoughts on “File Extension Using PHP”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.