How to specify environment variables when making a system call in PHP?

I would like to call system("foo")in PHP, but also specify the environment variables that "foo" will have access to when it is started. This is under Linux.

What is a simple trick for this?

+3
source share
1 answer

PutEnv ("UNIQID = $ uniqid");


Quotes from: PHP Manual, Feature 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
?> 
+3
source

Source: https://habr.com/ru/post/1718768/


All Articles