What is the best way to parse a filename in PHP and separate its components?

When parsing a filename in PHP, the best way to separate its components (such as the file name, extension, and directory path) is to use the built-in pathinfo() function. This function returns an associative array with keys like 'dirname', 'basename', 'extension', and 'filename', making it easy to access and manipulate the different parts of the filename.

$filename = "/path/to/file/example.txt";
$path_parts = pathinfo($filename);

$dirname = $path_parts['dirname'];
$basename = $path_parts['basename'];
$extension = $path_parts['extension'];
$filename = $path_parts['filename'];

echo "Directory: " . $dirname . "\n";
echo "Filename: " . $filename . "\n";
echo "Extension: " . $extension . "\n";