15

PHPのメソッドから出力を取得しようとしましproc_openたが、印刷すると空になりました。

$descriptorspec = 配列(
    0 => array("パイプ", "r"),
    1 => array("パイプ", "w"),
    2 => array("file", "files/temp/error-output.txt", "a")
);

$process = proc_open("time ./a a.out", $descriptorspec, $pipes, $cwd);

私が知っている限り、出力を得ることができますstream_get_contents()

echo stream_get_contents($pipes[1]);
fclose($パイプ[1]);

しかし、私はそれを行うことはできません..何か提案はありますか?

前にThx...

4

2 に答える 2

11

あなたのコードは多かれ少なかれ私にとってはうまくいきます。 timeその出力を出力するstderrので、その出力を探している場合は、 file を調べてくださいfiles/temp/error-output.txtstdoutパイプには、プログラムの$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
于 2011-05-16T11:34:03.967 に答える
10

これは を使用した別の例proc_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();
    }
}

これが役立つことを願っています=)

于 2013-03-25T04:44:33.613 に答える