How can one extract the original filename of a downloaded file when using file_get_contents() in PHP?

When using file_get_contents() in PHP to download a file, the original filename of the downloaded file is not directly accessible. However, you can extract the original filename from the HTTP headers of the response by using the get_headers() function. By parsing the "Content-Disposition" header, you can retrieve the original filename of the downloaded file.

$url = 'http://example.com/file-to-download.zip';
$headers = get_headers($url, 1);
$originalFilename = isset($headers['Content-Disposition']) ? basename($headers['Content-Disposition']) : 'downloaded_file.zip';

file_put_contents($originalFilename, file_get_contents($url));