开发者

Is it possible to run two commands and deal with both outputs on same page

I am running two linux commands with popen() in php. If I run 1, I can gather the output and process it fine. If I run two at the same time while funneling to the page with 2>&1, output gets jumbled. Is it possible to run two commands and deal with both outputs on same page?

I basically duplicated the bottom code for each command

$handle = popen ("-some lon开发者_Go百科g command 2>&1");

while (false !== ($char = fgetc($handle)))
{
  if ($char == "\r")
  {
    // You could now parse the $line for status information.
$line= "$line\n";
if (preg_match("/Duration: (.*?),/", $line, $matches)){
//do stuff

}

$line = "";
} else {
$line .= $char;
}
ob_flush();
flush();
}
pclose ($handle);
}


proc_open allows multiple, asynchronous commands execution; you would get the output through the stdout pipe.

PS: never duplicate code; use functions instead!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜