Get file extension from filename using PHP

pathinfo — Returns information about a file path

Description
pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )

pathinfo() returns information about path: either an associative array or a string, depending on options.

Parameters
path - The path to be parsed.

options
If present, specifies a specific element to be returned; one of PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION or PATHINFO_FILENAME.
If options is not specified, returns all available elements.

Return Value:
If the options parameter is not passed, an associative array containing the following elements is returned: dirname, basename, extension (if any), and filename.

Example:

<?php
$path_parts = pathinfo('/www/htdocs/inc/lib.inc.php');

echo $path_parts['dirname'], "\n";
echo $path_parts['basename'], "\n";
echo $path_parts['extension'], "\n";
echo $path_parts['filename'], "\n"; // since PHP 5.2.0?>


The above example will output:

/www/htdocs/inc
lib.inc.php
php
lib.inc

 Other method

The below function returns the extension of the given file name. Argument is the File name

function GetExtension($Filename)
{
   $Filename = strtolower($Filename) ;
   $Extension = explode(".", $Filename) ;
   $ExtensionNo = count($Extension)-1;
   $Extension = $Extension[$ExtensionNo];
   return $Extension;
}


Eg: GetExtension("feshers-2013-apply-1132.jpeg");

it return jpeg

No comments:

Post a Comment