I recently wrote a download script to allow the download of files from 1 server, after alot of hacking to get each browser to act as I would have hoped. In this method I used readfile().
My problem is I now want to download documents from another server and readfile seemed to struggle.
After reading around, I tried using fget to read the file in line by line, and serve the headers however I am getting corrupted files each time.
Code I'm using:
$filename = "http://download.example.net/test.pdf";
$handle = fopen($filename, "r");
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
#echo $buffer;
header('Expires: ' . $now);
header("Content-Transfer-Encoding: Binary");
header('Content-Length: ' . filesize($buffer));
header("Content-Type: application/force-download");
header('Content-Disposition: attachment; filename=' . basename($filename));
header('Content-Type: application/x-force-download; name='. $filename);
header('Pragma: no-cache');
header('Content-type: application/pdf');
}
fclose($handle);