PHPRO.ORG

Ever needed to simply get the file extension from the end of a file? This can be done with the pathinfo() function, but what was needed was the dot also in this case. Whilst pathinfo() gives the extension name, it does not return the dot. Keep in mind this function was created in an early version of php3 and is left for historical purposes. Both methods are provided for fun.


<?php

/*** example usage ***/
$filename 'filename.blah.txt';

/*** get the path info ***/
$info pathinfo($filename);

/*** show the extension ***/
echo $info['extenstion'];

?>

This second method does basically the same as the above, but uses string manipulation to get the extension and grabs the dot(.) character also.


<?php

/*** example usage ***/
$filename 'filename.blah.txt';

echo 
getFileExtension($filename);

/**
*
* @Get File extension from file name
*
* @param string $filename the name of the file
*
* @return string
*
**/
function getFileExtension($filename){
  return 
substr($filenamestrrpos($filename'.'));
}
?>