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:

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

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

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

$path_parts = pathinfo('/www/htdocs/inc/');

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:


Reference :