T. Brian Jones
T. Brian Jones

Reputation: 13502

Can you set a max file size to download using PHP's built in Curl functions?

When using Curl from the command line you can use the following command to limit downloaded file sizes to 250kb:

curl 'http://www.domain.com' --max-filesize 250000

I can't find a setting when using PHP's curl_init() and curl_setopt().

Am I missing something?

Upvotes: 4

Views: 2920

Answers (2)

T. Brian Jones
T. Brian Jones

Reputation: 13502

There is no way to do this with PHP's built in curl functions, without making a separate request to the webserver hosting the file.

Upvotes: 0

Sinthia V
Sinthia V

Reputation: 2093

You can if you use a callback-

CURLOPT_READFUNCTION Pass a function which will be called to read data. The callback function prototype:

string read_callback (resource ch, resource fd, long length)

The ch argument is CURL session handle. The fd argument is file descriptor passed to CURL by CURLOPT_INFILE option. The length argument is maximum length which can be returned. The function must return string containing the data which were read. If length of the data is more than maximum length, it will be truncated to maximum length. Returning anything else than a string means an EOF.

See curl_setopt.

Upvotes: 5

Related Questions