How does the function pathinfo() help in extracting information from file paths in PHP?

The pathinfo() function in PHP helps in extracting various components of a file path, such as the directory name, base name, extension, and filename. This function can be useful when working with file paths to retrieve specific information without having to manually parse the path string.

$file_path = '/path/to/file.txt';
$path_info = pathinfo($file_path);

echo 'Directory name: ' . $path_info['dirname'] . '<br>';
echo 'Base name: ' . $path_info['basename'] . '<br>';
echo 'Extension: ' . $path_info['extension'] . '<br>';
echo 'File name: ' . $path_info['filename'] . '<br>';