Home > PHP > 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 Responses

  1. Pingback : How to download a file using php code - Anil Labs

  2. Pingback : Code snippets of PHP and Javascript - Anil Labs

Leave a Reply

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