Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to partially download a remote file with cURL?

Tags:

Is it possible to partially download a remote file with cURL? Let's say, the actual filesize of the remote file is 1000 KB. How can I download only first 500 KB of it?

like image 954
Ken Avatar asked Jan 09 '10 09:01

Ken


People also ask

How do I use curl command to download a file?

How to download a file with curl command. The basic syntax: Grab file with curl run: $ curl https://your-domain/file.pdf. Get file using ftp or sftp protocol: $ curl ftp://ftp-your-domain-name/file.tar.gz.

How do I download a tar 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.

How do I download files using curl Windows?

To download a file with Curl, use the --output or -o command-line option. This option allows you to save the downloaded file to a local drive under the specified name. If you want the uploaded file to be saved under the same name as in the URL, use the --remote-name or -O command line option.

How do I download multiple files with curl?

Download multiple files simultaneously Instead of downloading multiple files one by one, you can download all of them simultaneously by running a single command. To download multiple files at the same time, use –O followed by the URL to the file that you wish to download. The above command will download both files.


2 Answers

You can also set the range header parameter with the php-curl extension.

$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.spiegel.de/'); curl_setopt($ch, CURLOPT_RANGE, '0-500'); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); curl_close($ch); echo $result; 

But as noted before if the server doesn't honor this header but sends the whole file curl will download all of it. E.g. http://www.php.net ignores the header. But you can (in addition) set a write function callback and abort the request when more data is received, e.g.

// php 5.3+ only // use function writefn($ch, $chunk) { ... } for earlier versions $writefn = function($ch, $chunk) {    static $data='';   static $limit = 500; // 500 bytes, it's only a test    $len = strlen($data) + strlen($chunk);   if ($len >= $limit ) {     $data .= substr($chunk, 0, $limit-strlen($data));     echo strlen($data) , ' ', $data;     return -1;   }    $data .= $chunk;   return strlen($chunk); };  $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.php.net/'); curl_setopt($ch, CURLOPT_RANGE, '0-500'); curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); curl_setopt($ch, CURLOPT_WRITEFUNCTION, $writefn); $result = curl_exec($ch); curl_close($ch); 
like image 54
VolkerK Avatar answered Sep 28 '22 05:09

VolkerK


Get the first 100 bytes of a document:

curl -r 0-99 http://www.get.this 

from the manual

make sure you have a modern curl

like image 29
SpliFF Avatar answered Sep 28 '22 07:09

SpliFF