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?
Show activity on this post. when curl is used to download a large file then CURLOPT_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.
Introduction : cURL is both a command line utility and library. One can use curl to download file or transfer of data/file using many different protocols such as HTTP, HTTPS, FTP, SFTP and more. The curl command line utility lets you fetch a given URL or file from the bash shell.
For very large size downloads (more than 2GB), we recommend that you use a Download Manager to do the downloading. This can make your download more stable and faster, reducing the risk of a corrupted file. Simply save the download file to your local drive.
To download you just need to use the basic curl command but add your username and password like this curl --user username:password -o filename. tar. gz ftp://domain.com/directory/filename.tar.gz . To upload you need to use both the –user option and the -T option as follows.
<?php set_time_limit(0); //This is the file where we save the information $fp = fopen (dirname(__FILE__) . '/localfile.tmp', 'w+'); //Here is the file we are downloading, replace spaces with %20 $ch = curl_init(str_replace(" ","%20",$url)); curl_setopt($ch, CURLOPT_TIMEOUT, 50); // write curl response to file curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // get curl response curl_exec($ch); curl_close($ch); fclose($fp); ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With