开发者

progress function and CURL multi

I have this code in which I have a list of remote files (for example images) to download using multi curl:

$urls = array('http://www.google.com/images/nav_logo82.png', 'http://static.php.net/www.php.net/images/php.gif');

function curl_progress_callback($a=0,$b=0,$c=0, $d=0)  {
    /*   ??    */
}

$save_to='./public/images/';

$conn = array();
$fp = array();
$mh = curl_multi_init();
foreach ($urls as $i => $url) {
    $g=$save_to.basename($url);
    if(!is_file($g)){
        $conn[$i]=curl_init($url);
        $fp[$i]=fopen ($g, "wb");
        curl_setopt ($conn[$i], CURLOPT_FILE, $fp[$i]);
        curl_setopt ($conn[$i], CURLOPT_HEADER ,0);
        curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,60);
        curl_setopt ($conn[$i], CURLOPT_MAXCONNECTS, 10);
        curl_setopt($conn[$i], CURLOPT_NOPROGRESS, false);
        curl_setopt($conn[$i], CURLOPT_PROGRESSFUNCTION, 'curl_progress_callback');
        curl_multi_add_handle ($mh,$conn[$i]);
    }
}
do {
    $n=curl_multi_exec($mh,$active);
}
while ($active);
for开发者_运维知识库each ($urls as $i => $url) {
    curl_multi_remove_handle($mh,$conn[$i]);
    curl_close($conn[$i]);
    fclose($fp[$i]);
}
curl_multi_close($mh);

The problem is that I want to make some sort of progress bar, so the php should give me informations about the progress of the downloading. For example I'd like to have a counter which increases by one every time a file download is finished (then it would be easy to write this counter on a .txt file and retrieve it every n seconds with ajax, to know how many files have been downloaded until that moment). For this purpose I have appended the function "curl_progress_callback", but I don't know what to put inside it to do what I want...

EDIT: In the end I managed to achieve this with a small function

function define_progress_callback($i) {
    global $conn;
    $flag = 1;
    curl_setopt($conn[$i], CURLOPT_PROGRESSFUNCTION, function ($a=0,$b=0,$c=0,$d=0) use($i, &$flag)  {
        if($b != 0 && $flag != 0 && $b/$a == 1) {
            /* Put here the code to execute every time that a download finishes */
            $flag = 0;
        }
    });
}



$save_to='./public/images/';

$conn = array();
$fp = array();
$mh = curl_multi_init();
foreach ($urls as $i => $url) {
    $g=$save_to.basename($url);
    $conn[$i]=curl_init($url);
    $fp[$i]=fopen ($g, "wb");
    curl_setopt ($conn[$i], CURLOPT_FILE, $fp[$i]);
    curl_setopt ($conn[$i], CURLOPT_HEADER ,0);
    curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,60);
    curl_setopt ($conn[$i], CURLOPT_MAXCONNECTS, 10);
    curl_setopt($conn[$i], CURLOPT_NOPROGRESS, false);
    define_progress_callback($i);
    curl_multi_add_handle ($mh,$conn[$i]);
}
do {
    $n=curl_multi_exec($mh,$active);
}
while ($active);
foreach ($urls as $i => $url) {
    curl_multi_remove_handle($mh,$conn[$i]);
    curl_close($conn[$i]);
    fclose($fp[$i]);
}
curl_multi_close($mh);

However I would be grateful is someone knows a better solution...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜