PHP caching a remote file that is force-downloaded

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
socket1
Forum Commoner
Posts: 82
Joined: Mon Dec 08, 2008 7:40 pm
Location: Shokan, New York

PHP caching a remote file that is force-downloaded

Post by socket1 »

I require help using PHP to cache a file that is force downloaded.

Say I go to http://example.com/dynamic.php?file=content&ext=wav The dynamic.php script forces the browser to download it but I want to cache the file using php.

Here is what I have so far

Code: Select all

 
$file = './content.wav'; // What I want the file to be downloaded as.
$remote_url = 'http://example.com/dynamic.php?file=content&ext=wav';
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $remote_url);
curl_setopt($handle, CURLOPT_TIMEOUT, 3);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($handle);
$fhandle = fopen($file, 'w+');
fwrite($fhandle, $output);
fclose($fhandle);
curl_close($handle);
 
This script works for caching the wav file to the server if the file is just at http://example.com/content.wav. But I am trying to get the script to recognize that the file is being forced downloaded and ignore it and just download it into the file anyway instead of forcing a download window on the browsers end.
Post Reply