PHP caching a remote file that is force-downloaded
Posted: Fri Feb 20, 2009 5:27 pm
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
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.
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);