dongniuxia8650 2016-04-11 15:08
浏览 330
已采纳

使用PHP和CURL计算下载文件的MD5

I have some cURL call that download a large file. I'm wondering if it is possible to calculate hash when the file is still downloading?

I think the progress callback function is the right place for accomplish that..

function get($urlget, $filename) {

        //Init Stuff[...]                   

        $this->fp = fopen($filename, "w+");
        $ch = curl_init();       


        //[...] irrelevant curlopt stuff

        curl_setopt($ch, CURLOPT_FILE, $this->fp);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_NOPROGRESS, 0);
        curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, array($this,'curl_progress_cb'));

        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

        $ret = curl_exec($ch);

        if( curl_errno($ch) ){
            $ret = FALSE;
        }

        curl_close($ch);

        fclose($this->fp);        

        return $ret;
    }

    function curl_progress_cb($dltotal, $dlnow, $ultotal, $ulnow ){
        //... Calculate MD5 of file here with $this->fp

    }
  • 写回答

2条回答 默认 最新

  • dslpofp041310584 2016-04-11 15:12
    关注

    Its possible to calculate md5 hash of partially downloaded file, but it does not make too much sense. Every downloaded byte will change your hash diametrally, what is the reason behind going with this kind solution?

    If you need to have md5 hash for entire file than the answer is NO. Your program has to first download the file and then generate the hash.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器