2011-05-16 13 views
7

PHP에서 proc_open 메서드의 결과를 얻으려고했지만 인쇄 할 때 비어 있습니다.proc_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); 

는 한 내가 아는 한, 나는 stream_get_contents()

 
echo stream_get_contents($pipes[1]); 
fclose($pipes[1]);

과 출력을 얻을 수 있습니다하지만 난 그 .. 어떤 제안을 할 수 없어?

Thx before ...

+0

이 하하, 코드가 실제로 저 방법 proc_open 작품을 이해했다. – Yoshiyahu

+0

@Yoshiyahu 하하, 그걸 알고 기꺼이 .. :) –

답변

6

코드가 다소 효과가 있습니다. time은 출력을 stderr으로 출력하므로 출력을 찾으려면 파일 files/temp/error-output.txt을보십시오. stdout 파이프 $pipes[1]에는 프로그램 ./a의 출력 만 포함됩니다.

내 생식은 :

[[email protected] 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]); 

?> 

[[email protected] tmp]$ php proc.php 

a.out here. 

[[email protected] tmp]$ cat /tmp/error-output.txt 

real 0m0.001s 
user 0m0.000s 
sys  0m0.002s 
+0

Thx 너의 도움을 ... 나는 내 stderr를 확인하지 않는다 ... 마침내, 나는 그것을 점검하고 출력을 발견했다 ... thanks .. –

4

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(); 
    } 
} 

희망이 = 도움)

+0

왜 ob_flush가 필요하고 플러시가 필요합니까? – nidhimj22

+0

요약 : "flush()는 웹 서버의 버퍼링 체계를 재정의 할 수 없으며 브라우저의 클라이언트 측 버퍼링에 영향을 미치지 않으며 PHP의 사용자 공간 출력 버퍼링 메커니즘에도 영향을 미치지 않습니다. 출력 버퍼를 사용하고 있다면 ob 출력 버퍼를 비우려면 ob_flush()와 flush()를 모두 호출해야합니다. " 출처 : http://www.php.net/manual/en/function.flush.php –