Are there any PHP functions or methods that can help with breaking down a filename into its constituent parts?

When working with filenames, it can be helpful to break them down into their constituent parts such as the file name, extension, and directory path. PHP provides several functions that can help with this task, such as pathinfo() and basename(). These functions can parse a filename and return an array containing its various parts.

$filename = "example.txt";

// Using pathinfo() function to get the filename components
$file_info = pathinfo($filename);

// Accessing the individual components
$file_name = $file_info['filename'];
$extension = $file_info['extension'];
$directory = $file_info['dirname'];

// Output the results
echo "File Name: " . $file_name . "\n";
echo "Extension: " . $extension . "\n";
echo "Directory: " . $directory . "\n";