2012-04-30 34 views
0

我試圖運行使用proc_open()函數的進程。正如在頁面上指定的那樣 - 我提供了自定義環境變量並試圖打印出來。它顯示了我提供的所有變量+總是3個變量:'SHLVL','PWD','_ ='。我想打印/只使用我提供的環境變量。這3個功能總是存在嗎?有沒有辦法只提供變量?這全部在Linux和PHP5下。PHP proc_open環境變量

//Here is the code to clarify : 
$descriptorspec = 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 
); 

$env = array('MY_VAR' => 'FOO'); 

$process = proc_open('./run.php', $descriptorspec, $pipes, $cwd, $env); 

if (is_resource($process)) { 

fwrite($pipes[0], escapeshellcmd($args)); 
fclose($pipes[0]); 

$output = ""; 
while (!feof($pipes[1])) { 
    $output .= fgets($pipes[1]); 
} 

print "Output is $output \n"; 
    fclose($pipes[1]); 
    $return_value = proc_close($process); 

} 

謝謝。

回答

0

您可以爲您的環境變量命名空間,例如PHP_MYVAR而不是MYVAR。這樣,您可以根據通用前綴PHP_進行過濾。

0

這三個變量是由shell創建的。如果你沒有打開一個shell,它們將不會被創建。

+0

我不明白你爲什麼*要*打開一個shell,但如果你做*然後就沒有辦法繞過它。 –

+0

那...不......在任何地方調用shell ... –

+0

而且?它仍然不會在任何地方調用shell。 –

0

它只是與Linux有關。它的工作原理與Solaris一樣。我添加了正則表達式過濾器來刪除這些額外的變量。