I want to change the default system $PATH var for the period of php script execution, but it for some reason fails.
Im trying the following:
<?php
$lastline = system('export PATH=$PATH:/customBin;',$return) OR die("why do i die all the time");
echo $lastline;
?>
But it obviously dies all the time. (tried on both RHEL & Debian linux distros, php version 5.3.xx, other system commands work fine (cat, ls, etc) neither of these work: export PATH=$PATH:/customBin;
OR export PATH=$PATH:/customBin:
OR export PATH=/customBin
, all of these work in the shell tho..)
Any help is appreciated. thanks.
EDIT: Above code is wrong, solution is to use putenv('PATH=$PATH:/customBin');
You might have better luck using putenv to change environment variables from PHP. Just fetch the current value with getenv, add whatever you like and finally call putenv to alter the value.
System's return value
(not the return parameter) is the last line of the command(s)' output or boolean FALSE
if it fails. In your case it probably succeeded (you have int 0
in your $return variable for a successful exit) and returned nothing (an empty string perhaps) which evaluates to false in your or statement and the die gets executed. If you want to check for a successful export, try
system('export PATH=$PATH:/customBin;', $return);
$return === 0 OR die('...');
or if you want to use system's failure handler:
(system('export PATH=$PATH:/customBin;') !== FALSE) or die('...');
Haven't tested this, your results may vary.
Your approach with system
is creating a new shell environment, setting the PATH in that, then leaving it.
In addition, your check for success is broken. The call returns an empty string, which then fails your conditional check.
As far as I am aware, you can't manipulate the environment that invoked php.