2017-04-21 90 views
0

我正在使用系统卷曲命令通过php下载文件。这一切都工作正常,但我现在试图显示一些进展,因为它下载。下载时的PHP /卷曲进度

curl命令和php称为是:

$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); 

这是使用AJAX调用,并且输出显示在一个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); 
       } 
      } 
     }) 

这工作,但结果在msg div舒是不一致。 我可以得到:

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

我得到的部分结果即:5.3% instead of 15.3%时,得到相同的输出即内的多个结果:12.5% & 13.2%

反正是有标准化这个所以我只通过至100获得0% %?

感谢

回答

1

修改PHP的str_replace函数来

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

那么你只能得到的百分比,没有前面的空格,您可以只插入到容器无编辑。

实施例: https://3v4l.org/OGWqU

+0

感谢。实际上我已经完成了,但你的答案与我所提出的答案基本相同。我添加的唯一的其他东西是仅在5个字符时回显结果。这个结果是一个小小的停顿,然后这个过程开始于'10.0%'这适用于有几个输入丢失数字的情况。 – Tom