Php 如何获取proc_open()的输出
我试图从php中的Php 如何获取proc_open()的输出,php,linux,proc-open,Php,Linux,Proc Open,我试图从php中的proc\u open方法中获取输出,但是,当我打印它时,结果是空的 $descriptorspec = array( 0 => array("pipe", "r"), 1 => array("pipe", "w"), 2 => array("file", "files/temp/error-output.txt", "a") ); $process = proc_open("time ./a a.out", $descriptorspec, $pi
proc\u open
方法中获取输出,但是,当我打印它时,结果是空的
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", "files/temp/error-output.txt", "a")
);
$process = proc_open("time ./a a.out", $descriptorspec, $pipes, $cwd);
但是我不能那样做。。
有什么建议吗
Thx之前 你的代码或多或少对我有用
time
将其输出打印到stderr
中,因此如果要查找该输出,请查看文件files/temp/error output.txt
。stdout
pipe$pipes[1]
将只包含程序/a
的输出
我的报告:
[edan@edan tmp]$ cat proc.php
<?php
$cwd='/tmp';
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", "/tmp/error-output.txt", "a") );
$process = proc_open("time ./a a.out", $descriptorspec, $pipes, $cwd);
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
?>
[edan@edan tmp]$ php proc.php
a.out here.
[edan@edan tmp]$ cat /tmp/error-output.txt
real 0m0.001s
user 0m0.000s
sys 0m0.002s
[edan@edantmp]$cat proc.php
[edan@edantmp]$php proc.php
a、 在这里。
[edan@edantmp]$cat/tmp/error-output.txt
实0.001s
用户0.000s
系统0m0.002s
这是proc\u open()
的另一个示例。
在本例中,我使用Win32 ping.exe命令。CMIIW
set_time_limit(1800);
ob_implicit_flush(true);
$exe_command = 'C:\\Windows\\System32\\ping.exe -t google.com';
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout -> we use this
2 => array("pipe", "w") // stderr
);
$process = proc_open($exe_command, $descriptorspec, $pipes);
if (is_resource($process))
{
while( ! feof($pipes[1]))
{
$return_message = fgets($pipes[1], 1024);
if (strlen($return_message) == 0) break;
echo $return_message.'<br />';
ob_flush();
flush();
}
}
设置时间限制(1800);
ob_隐式_刷新(真);
$exe_command='C:\\Windows\\System32\\ping.exe-t google.com';
$descriptorspec=数组(
0=>数组(“管道”、“r”),//stdin
1=>array(“pipe”,“w”),//stdout->我们使用这个
2=>数组(“管道”,“w”)//stderr
);
$process=proc_open($exe_命令,$descriptorspec,$pipes);
如果(是_资源($process))
{
而(!feof($pipes[1]))
{
$return_message=fgets($pipes[1],1024);
如果(strlen($return_message)==0)中断;
回显$return_消息。“
”;
ob_flush();
冲洗();
}
}
希望这有助于=)谢谢您的帮助。。。我没有检查我的标准。。。最后,我检查了一下,发现输出。。。谢谢..哈哈,你的代码实际上让我明白了proc_open是如何工作的。@Yoshiyahu哈哈,很高兴知道这一点...)为什么在这里需要ob_flush和flush?总而言之:“flush()可能无法覆盖web服务器的缓冲方案,它对浏览器中的任何客户端缓冲都没有影响。它也不会影响PHP的用户空间输出缓冲机制。这意味着您必须同时调用ob_flush()和flush()如果正在使用ob输出缓冲区,则刷新这些缓冲区。来源:
set_time_limit(1800);
ob_implicit_flush(true);
$exe_command = 'C:\\Windows\\System32\\ping.exe -t google.com';
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout -> we use this
2 => array("pipe", "w") // stderr
);
$process = proc_open($exe_command, $descriptorspec, $pipes);
if (is_resource($process))
{
while( ! feof($pipes[1]))
{
$return_message = fgets($pipes[1], 1024);
if (strlen($return_message) == 0) break;
echo $return_message.'<br />';
ob_flush();
flush();
}
}