Getting Filenames From Path

Sometimes you have a situation when you want filename without extension and you don’t know extension then you can use pathinfo().

Example File info:

$file='/www/htdocs/inc/lib.inc.php';
$ext = pathinfo($file, PATHINFO_EXTENSION);  // php
$filename = pathinfo($file, PATHINFO_FILENAME);  //  lib.inc
$dirname = pathinfo($file, PATHINFO_DIRNAME);  //   /www/htdocs/inc
$fileFullName = pathinfo($file, PATHINFO_BASENAME); //   lib.inc.php

pathinfo() function can be used for all purposes. So it is best.

Here is an example of using pathinfo() without predefined constants

<?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

Reference : http://php.net/manual/en/function.pathinfo.php