View Full Version : Download remote file and stream to user using cURL

09-12-2011, 05:04 PM
what this code does is to download a-large-file.zip from example.com and save to hard disk.

instead of saving to hard disk, how do i stream the file to user?

$url = 'http://www.example.com/a-large-file.zip';
$path = '/path/to/a-large-file.zip';

$fp = fopen($path, 'w');

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FILE, $fp);

$data = curl_exec($ch);


09-12-2011, 05:11 PM
You still need to save it to the hard disk (otherwise you're in danger of eating up all the ram OR getting a message from php about the memory being exhausted).

Then you need to reopen it and in a loop fread it and print it.