2017-04-21 86 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