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

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : [email protected]

You may also like...

4 Responses

  1. Can u help me.

    how to redirect URL’s using .htaccess file

    like this

    http://domain.com/index.php?id=srinivas&page=2

    to

    http://domain.com/srinivas?page=2

  2. Mustafa Ali says:

    more simpler:
    strtolower(end(explode(‘.’,$filename));

    also worth reading http://php.net/manual/en/function.pathinfo.php

  1. November 8, 2011

    […] website have list of files and users can download those files. In my previous post i have explain how to get extension of file, using that post get the […]

  2. October 29, 2013

    […] Code We can get the two type of snippets from earlier file extension using PHP […]

Leave a Reply

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