Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading a large file using curl

Tags:

php

curl

download

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?

like image 690
kusanagi Avatar asked Jun 20 '11 09:06

kusanagi


People also ask

How do I download large files with curl?

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.

Can I use curl to download a file?

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.

What is the easiest way to download large files?

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.

How do I download a Tar GZ file with curl?

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.


1 Answers

<?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); ?> 
like image 147
TheBrain Avatar answered Sep 23 '22 13:09

TheBrain