我正在尝试使用 proc_open() 运行进程功能。正如页面上所指定的 - 我提供了自定义环境变量并尝试打印出来。它显示了我提供的所有变量+始终为 3 个变量:“SLVL”、“PWD”、“_=”。我想仅打印/使用我提供的环境变量。这3个总是有这个功能吗?有什么办法只提供变量吗?这都是在 Linux 和 PHP5 下进行的。
//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);
}
谢谢。
最佳答案
您可以命名环境变量,例如PHP_MYVAR
而不是 MYVAR
。这样您就可以根据公共(public)前缀 PHP_
进行过滤。
关于PHP proc_open 环境变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10378660/