Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a maximum size limit to PHP cURL downloads?

Tags:

php

curl

Is there a maximum size limit to PHP cURL downloads? ie. will cURL quit when transfer reaches a certain file limit?

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$data = curl_exec($ch);

It's for a site that downloads remote images. I want to ensure that cURL will stop when it reaches a certain limit.

Also my research shows getimagesize() downloads the image, to return its size so its not an option.

like image 388
Sojan Jose Avatar asked Jul 14 '13 16:07

Sojan Jose


People also ask

What is Curl_init PHP?

The curl_init() function will initialize a new session and return a cURL handle. curl_exec($ch) function should be called after initialize a cURL session and all the options for the session are set. Its purpose is simply to execute the predefined CURL session (given by ch).

Is PHP cURL secure?

Curl is as secure as a normal HTTP request.

Is cURL a PHP library?

cURL is a PHP library and command-line tool (similar to wget) that allows you to send and receive files over HTTP and FTP. You can use proxies, pass data over SSL connections, set cookies, and even get files that are protected by a login.


2 Answers

There is. It's the PHP memory limit, I presume. As the download is done in memory...

But CURLOPT_FILE and CURLOPT_WRITEHEADER^ are your friends as they allow you to reroute the cURL download to streams. This allows you to create tmpfile() temporary streams (stream_get_meta_data() gives you the file path) and download to them. And downloading directly to drive lifts the memory limitations.

Once the download completes, you get to read those files and do what you wish with them.

like image 92
CodeAngry Avatar answered Oct 19 '22 22:10

CodeAngry


The server does not honor the Range header. The best you can do is to cancel the connection as soon as you receive more data than you want. Example:

<?php
$curl_url = 'http://steamcommunity.com/id/edgen?xml=1';
$curl_handle = curl_init($curl_url);

$data_string = "";
function write_function($handle, $data) {
global $data_string;
$data_string .= $data;
if (strlen($data_string) > 1000) {
    return 0;
}
else
    return strlen($data);
} 

curl_setopt ($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($curl_handle, CURLOPT_CONNECTTIMEOUT, 2);
curl_setopt ($curl_handle, CURLOPT_WRITEFUNCTION, 'write_function');

curl_exec($curl_handle);

echo $data_string;

Perhaps more cleanly, you could use the http wrapper (this would also use curl if it was compiled with --with-curlwrappers). Basically you would call fread in a loop and then fclose on the stream when you got more data than you wanted. You could also use a transport stream (open the stream with fsockopen, instead of fopen and send the headers manually) if allow_url_fopen is disabled.

like image 40
Dr ZIZO Avatar answered Oct 19 '22 22:10

Dr ZIZO