How to get file extension (without parsing file name)

Posted by nikola, With 0 Comments, Category: PHP,

Get file extension without use of explode function of preg functions

You can use pathinfo function - it will return an array with elements like:

  • dirname
  • basename
  • extension
  • filename

Reading those values you will get proper values.

<?php
 $path_parts = pathinfo('/var/www/htdocs/index.php');
 echo $path_parts['dirname'];
 echo $path_parts['basename'];
 echo $path_parts['filename']; // since PHP 5.2.0
 echo $path_parts['extension'];
 ?>