PHP proc_open 环境变量

标签 php linux environment-variables process proc-open

我正在尝试使用 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/

相关文章:

python - 使用 boto 将环境变量添加到 ec2 实例

php - 未定义索引 PHP : Can't Find Form Values

php - Symfony 6 登录不运行身份验证

php - 检查登录时是否未激活激活码。 "ONLY"激活码

linux - 通过 ssh 在 OpenCL 中的浮点错误

linux - 为多台机器保存 docker 配置的 Git 策略

linux - Derby 安 : my service doesn't autostart

php - 通过 Laravel 更新 .env 值

php - Pchart - 错误轴上的折线图

java - 在 Maven 配置文件中设置系统变量