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

使用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 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同