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

时间:2009-09-29 03:28:29

标签: php linux unix

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

有什么简单的伎俩吗?

1 个答案:

答案 0 :(得分:3)

运行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
?>