dongtui2029 2015-06-25 17:40 采纳率: 0%
浏览 237
已采纳

PHP - CURLOPT_BUFFERSIZE被忽略

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 ."";
}

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.

  • 写回答

1条回答 默认 最新

  • duanliao5995 2015-06-25 18:20
    关注

    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) ."";
            }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型