user3119384
user3119384

Reputation: 339

PHP - CURLOPT_BUFFERSIZE ignored

I would like to execute the callback function every X bytes uploaded, but I don't understand why php keeps calling the callback function way way more often.

here is my code:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$converter); 
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'callback');
curl_setopt($ch, CURLOPT_BUFFERSIZE, 10485764);
$result=curl_exec ($ch); 

//$info = curl_getinfo($ch);
//print_r($info);
curl_close ($ch);

function callback($resource, $download_size, $downloaded, $upload_size, $uploaded) {
    echo $uploaded . '/' . $upload_size ."\r";
}

The file to upload is around 68 MB, the callback function should get executed 68 times (10485764 bytes = 1 MB), but it gets executed around 9k times...

The function should write the progress in a mysql db, that's why I need it to get executed less time.

Upvotes: 0

Views: 4040

Answers (1)

user3119384
user3119384

Reputation: 339

As Barman stated, CURLOPT_BUFFERSIZE is related to download and won't work for upload.

The solution is to check the size and do something only if a certain amount of byte has been uploaded.

Exemple:

$i= 0;
$up = 0;

function callback($resource, $download_size, $downloaded, $upload_size, $uploaded) {
        global $i, $up;
        if ($uploaded > ($up + 1048576)){
            $i++;
            $up = $uploaded + 1048576;
            echo $i . ' => ' . formatBytes($uploaded) . '/' . formatBytes($upload_size) ."\r";
        }
}

Upvotes: 2

Related Questions