dpbl91234 2009-07-14 14:03
浏览 194
已采纳

从命令行捕获进度

% Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 12.4M 100 12.4M 0 0 4489k 0 0:00:02 0:00:02 --:--:-- 4653k

The above is a CURL output from the command line when download the file. I have captured this using PHP like so, but I am having trouble working out how to use pre_match to extract the percentage done.

$handle = popen('curl -o '.VIDEOPATH.$fileName.'.flv '.$url, 'rb');

while(!feof($handle))
{
    $progress = fread($handle, 8192);
    //I don't even know what I was attempting here
    $pattern = '/(?<Total>[0-9]{1,3}\.[0-9]{1,2})% of (?<Total>.+) at/';
    //divide received by total somehow, then times 100
    if(preg_match_all($pattern, $progress, $matches)){
    fwrite($fh, $matches[0][0]."
");
    }

} 

How can I do this? Please note, I have no idea what I am doing with the above preg_match_all!

Thanks

Update

Thanks to the help of ylebre. I have this so far.

$handle = popen('curl -o '.VIDEOPATH.$fileName.'.flv '.$url.' 2>&1', 'rb');//make sure its saved to videos

while(!feof($handle))
{

    $line = fgets($handle, 4096); // Get a line from the input handle
    echo '<br>Line'.$line.'<br>';
    $line = preg_replace("/s+/", " ", $line); // replace the double spaces with one
    $fields = explode(" ", $line); // split the input on spaces into fields array
    echo '<br>Fields: '.$fields[0];
    fwrite($fh, $fields[0]); // write a part of the fields array to the output file

} 

I get this output to the browser:


Line % Total % Received % Xferd Average Speed Time Time Time Current

Fields: Line Dload Upload Total Spent Left Speed

Fields: Line 0 1340k 0 4014 0 0 27342 0 0:00:50 --:--:-- 0:00:50 27342 41 1340k 41 552k 0 0 849k 0 0:00:01 --:--:-- 0:00:01 1088k 100 1340k 100 1340k 0 0 1445k 0 --:--:-- --:--:-- --:--:-- 1711k

Fields: Line


How do I extract the percentage part only? Maybe CURL can do this by itself - hmm will ask a question on this.

  • 写回答

2条回答 默认 最新

  • dtcmadj31951 2009-07-14 14:20
    关注

    The progress that is showing up is probably updating the information in the same spot, so it will help if you know what you are parsing exactly.

    The next step I recommend is taking one line of input, and trying to get the regexp to work on that.

    You could also just split the string at the spaces if I'm reading the output correctly. If you start out by replacing all the double spaces into one. After that you can use explode() to get an array with the values, which you can print_r to take a peek what is inside.

    This would be something like:

    $line = fgets($handle, 4096); // Get a line from the input handle
    $line = preg_replace("/s+/", " ", $line); // replace the double spaces with one
    $fields = explode(" ", $line); // split the input on spaces into fields array
    fwrite($fh, $fields[0]); // write a part of the fields array to the output file
    

    As long as the ordering in the fields remains the same, your resulting array should give you a consistent result.

    Hope this helps!

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

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法