I need to download remote file using curl.
Here's the sample code I have:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$st = curl_exec($ch);
$fd = fopen($tmp_name, 'w');
fwrite($fd, $st);
fclose($fd);
curl_close($ch);
But it can't handle big files, because it reads to memory first.
Is it possible to stream the file directly to disk?
when
curl
is used to download a large file thenCURLOPT_TIMEOUT
is the main option you have to set for.CURLOPT_RETURNTRANSFER
has to be true in case you are getting file like pdf/csv/image etc.You may find the further detail over here(correct url) Curl Doc
From that page:
I use this handy function:
By downloading it with a 4094 byte step it will not full your memory
Usage:
You can then check if everything is ok with:
Find below code if you want to download the contents of the specified URL also want to saves it to a file.
If you want to downloads file from the FTP server you can use php FTP extension. Please find below code:
You can use this function, which creates a tempfile in the filesystem and returns the path to the downloaded file if everything worked fine: