How can pathinfo() be used to efficiently split file names in PHP?
When working with file names in PHP, the pathinfo() function can be used to efficiently split the file name into its component parts such as the filename, extension, and directory path. This can be useful for tasks like renaming files, organizing file uploads, or extracting information from file names.
$file = "example.txt";
$path_parts = pathinfo($file);
$filename = $path_parts['filename'];
$extension = $path_parts['extension'];
$directory = $path_parts['dirname'];
echo "Filename: " . $filename . "\n";
echo "Extension: " . $extension . "\n";
echo "Directory: " . $directory . "\n";