如何设置最大大小限制为php curl下载 [英] how to set a maximum size limit to php curl downloads

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

问题描述

是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);

它用于下载远程图像的网站。想要确保curl在达到一定限制时停止。
我的研究显示 getimagesize()下载图片,返回其大小。所以它不是一个选项。

its for a site that downloads remote images. 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.

推荐答案

我有另一个答案,更好地处理这种情况,留给后代。

I have another answer that addresses the situation even better to leave here for posterity.

CURLOPT_WRITEFUNCTION 对此有用,但 CURLOPT_PROGRESSFUNCTION 是最好的

// 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


接受五个参数的回调。


$ b b

我的本​​地测试仅显示四(4)个参数,因为 1st(句柄)不存在。

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

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