PHP Code Snippets Episode 1 – Getting Filetype

Over the course of the next couple of weeks I’m going to place a couple of blogs with a few little PHP Snippets.

These are here more for reference however they are open for discussion so feel free to reply with alternativesor comments or short cuts for my code.

I will try to describe exactly how it works as I go along.

So with this little PHP function you can put a filename into it and it will return you the FILETYPE

function file_extension($filename){
         $path_info = pathinfo($filename);
         return $path_info['extension'];

So the funtion is quite simply using the PHP function pathinfo()

As you can see you put the name in and pathinfo makes an array and from that you pull the ‘extension’

To call this function :

$filename = 'thisisanexample.pdf' ;
$filetype = file_extension('$filename');
echo $filetype;

Simple as that !

This will make the $filetype variable : pdf

Leave a Response