Nomisoft
Menu

Downloading large files with PHP

07th July 2015

I needed to download a huge 1gb+ file in PHP. I was using curl but was receiving out of memory errors as PHP was trying to load the entire file into a variable like so:


$response = curl_exec($ch);

The way around this was to use the CURLOPT_FILE option to write the response directly to a file rather than holding it in a variable in memory


$fp = fopen('file.ext', 'w+');
$ch = curl_init('http://example.com/file.ext');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_exec($ch);
curl_close($ch);

Also note that the CURLOPT_RETURNTRANSFER option must be set before the CURLOPT_FILE option, i.e


curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $fp);

Works however the following does not


curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);