I need to set some environment variable by php and access them from windows cmd. From cmd I call php with call php\php.exe install.php 0
then install.php
will set some environment variable. when execution of install.php
is finished then I tried to get those variables from parent cmd. But cmd can not get those values.
here is my install.php
:
<?php
$config = json_decode(file_get_contents('tmp/config.json'), true);
foreach ($config[$argv[1]] as $segment=>$details){
putenv("targetFolder=$segment");
putenv("targetLink=$details[link]");
}
echo getenv('targetFolder');
here is result:
%targetFolder%
should return servers
With setenv, you able to set variables for the current process, and cmd.exe
is the different, parent one. You cannot change the env of parent process without hacks. You probably should rewrite the script to put necessary set ENV=VALUE
lines into some temporary batch file and then call it.
<?php
$config = json_decode(file_get_contents('tmp/config.json'), true);
$tmpBatch = fopen('tmp/setenv.bat', 'w');
foreach ($config[$argv[1]] as $segment=>$details){
fwrite($tmpBatch, "set targetFolder=$segment");
fwrite($tmpBatch, "set targetLink=$details[link]");
}
fclose($tmpBatch);
And then
php\php.exe install.php
call tmp\setenv.bat
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.