gpt4 book ai didi

php - 在 PHP 中进行系统调用时如何指定环境变量?

转载 作者:搜寻专家 更新时间:2023-10-31 20:56:21 24 4
gpt4 key购买 nike

我想在 PHP 中调用 system("foo"),但还要指定“foo”在运行时可以访问的环境变量。这是在 Linux 下。

有什么简单的技巧吗?

最佳答案

putenv("UNIQID=$uniqid");


引用自:The PHP Manual, Function Reference, putenv

For example, if a particular system command required a special value
of the environment variable LD_LIBRARY_PATH to execute successfully,
then the following code might be used on a *NIX system:

<?php
$saved = getenv("LD_LIBRARY_PATH"); // save old value
$newld = "/extra/library/dir:/another/path/to/lib"; // extra paths to add
if ($saved) { $newld .= ":$saved"; } // append old paths if any
putenv("LD_LIBRARY_PATH=$newld"); // set new value
system("mycommand -with args"); // do system command;
// mycommand is loaded using
// libs in the new path list
putenv("LD_LIBRARY_PATH=$saved"); // restore old value
?>

关于php - 在 PHP 中进行系统调用时如何指定环境变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1490437/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com