PHP读取控制台输出

时间:2009-08-28 09:13:55

标签: php linux console

好的,所以这就是事情。我需要读取输出(您通常在Linux控制台中看到的输出)。我最大的问题是我不需要读取线性执行的输出,而是需要像wget http://ubuntu.com/jaunty.iso那样显示它的ETA。

此外,工作流程如下:

S - 网络服务器

C 1 - S的内联网中的computer1

C 2 - S的内联网中的计算机2

等等。

用户连接到连接到S x C,然后启动wgettop或其他控制台日志记录命令(根据用户的请求) )。用户可以在C x 时看到“控制台日志”,而wget下载指定的目标。

这是合理的吗?可以在不使用服务器/客户端软件的情况下完成吗?

谢谢!

2 个答案:

答案 0 :(得分:3)

你会想要使用php函数proc_open - 你可以指定一个管道数组(stdin,如果你在控制台上,通常会附加到键盘上,std out,和stderr,通常都会打印到显示器上)。然后,您可以控制给定程序的输入/输出内容

以此为例:

$pipes = array();
$pipe_descriptions = array(
   0 => array("pipe", "r"),  // stdin is a pipe that the child will read from
   1 => array("pipe", "w"),  // stdout is a pipe that the child will write to
   2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);

$cmd = "wget $url";

$proc_handle = proc_open($cmd, $pipe_descriptions, $pipes);

if(is_resource($proc_handle))
{
   // Your process is running. You may now read it's output with fread($pipes[1]) and fread($pipes[2])
   // Send input to your program with fwrite($pipes[0])
   // remember to fclose() all pipes and fclose() the process when you're done!

答案 1 :(得分:0)

您是否正在使用一些现有的PHP代码?