如何设置PHP cURL下载的最大大小限制? [英] How to set a maximum size limit to PHP cURL downloads?

查看:514
本文介绍了如何设置PHP cURL下载的最大大小限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

PHP cURL下载是否有最大大小限制? IE.传输达到特定文件限制后,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);

它是用于下载远程图像的站点.我想确保当cURL达到一定限制时将停止.

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

我的研究还显示getimagesize()下载图像,以返回其大小,因此无法选择.

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

推荐答案

我还有一个解决这个问题的更好的答案,就是离开这里后代.

CURLOPT_WRITEFUNCTION对此非常有帮助,但 CURLOPT_PROGRESSFUNCTION是最好的.

CURLOPT_WRITEFUNCTION is good for this but CURLOPT_PROGRESSFUNCTION is the best.

// We need progress updates to break the connection mid-way
curl_setopt($cURL_Handle, CURLOPT_BUFFERSIZE, 128); // more progress info
curl_setopt($cURL_Handle, CURLOPT_NOPROGRESS, false);
curl_setopt($cURL_Handle, CURLOPT_PROGRESSFUNCTION, function(
    $DownloadSize, $Downloaded, $UploadSize, $Uploaded
){
    // If $Downloaded exceeds 1KB, returning non-0 breaks the connection!
    return ($Downloaded > (1 * 1024)) ? 1 : 0;
});

请记住,即使 PHP.net声明 ^ 用于CURLOPT_PROGRESSFUNCTION:

接受五个参数的回调.

我的本​​地测试仅具有四(4)个参数,因为没有第一个(句柄).

My local tests have featured only four (4) parameters as the 1st (handle) is not present.

这篇关于如何设置PHP cURL下载的最大大小限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆