2012-04-30 33 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一样。我添加了正则表达式过滤器来删除这些额外的变量。