duanruinong0619 2017-04-21 08:43
浏览 93
已采纳

下载时PHP / Curl进度

I'm using a system curl command via php to download a file. That is all working fine, but I'm now trying to show some progress as it's downloaded.

The curl command and php called is:

$a = popen("cd user; curl -O -# remote.server.com/filename.tar.gz 2>&1", 'r');

ob_flush();flush();
while($b = fgets($a, 64)) {
    ob_flush();flush();
        if (preg_match('~\b(error|fail|unknown)\b~i',$b)) {
            echo "error^$b";
            exit;
        }
        echo str_replace('#','',$b);
       ob_flush();flush();
}
pclose($a);

This is called using ajax and the output is displayed in a div:

var last_response_len = false;
        $.ajax(url, {
            xhrFields: {
                onprogress: function(e)
                {
                    var this_response, response = e.currentTarget.response;
                    if(last_response_len === false)
                    {
                        this_response = response;
                        last_response_len = response.length;
                    }
                    else
                    {
                        this_response = response.substring(last_response_len);
                        last_response_len = response.length;
                    }
                    $(upg).show();
                    console.log(this_response)
                    var count = this_response.match(/%/g);
                    if (count !== null && count.length != 2) $(msg).html(this_response);
               }
            }
        })

This works but the results showin in the msg div are not consistent. I may get :

1%
5%
12.1%
12.5% 13.2%
14.2%
5.3%
16.7%

I get partial results ie: 5.3% instead of 15.3%, I get multiple results within the same output ie: 12.5% & 13.2%

is there anyway to standardise this so I only get 0% through to 100% ?

Thanks

  • 写回答

1条回答 默认 最新

  • 普通网友 2017-04-21 09:00
    关注

    change the php str_replace to

    str_replace(['#', ' '], '', $b);
    

    Then you get only the percentage, without the preceding blanks, which you can just insert to the container without editing.

    Example: https://3v4l.org/OGWqU

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题