How can the basename function be used to extract the file name from a URL in PHP?

To extract the file name from a URL in PHP, you can use the basename function. This function takes a string (in this case, the URL) and returns the last component of the path, which in this case would be the file name. By using basename, you can easily extract the file name from a URL without having to manually parse the string.

$url = "https://www.example.com/path/to/file.txt";
$fileName = basename($url);
echo $fileName; // Output: file.txt