Tip: Getting extension/name of a file

Here is the easiest way to get extension of a file in php

<?php
    $paht='uploads/images/fil1.jpg';
    $parts=pathinfo($path);
    $ext=$parts['extension'];
    echo "extension=".$ext;		//outputs jpg
 ?>

and if you want to get the name of a file, you can use the following code

<?php
    $file = 'image.jpg';
    $info = pathinfo($file);
    $file_name =  basename($file,'.'.$info['extension']);
    echo $file_name; // outputs 'image'
 ?>

 

More PHP tips