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));
Related Questions
- What are the potential risks of using IP-based pseudosessions instead of traditional sessions in PHP?
- What are the security implications of using the exec() function in PHP to execute command line commands for image conversion?
- What is the recommended method for creating thumbnails in PHP to maintain image quality?