Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set $PATH in PHP?

I am currently working on my own little project, but I have a little problem: I want to set the $PATH environment variable to ./bin, so that when I use exec() and similar functions, it would only search for binary files in that directory (unless I explicitly tell it otherwise).

I have already tried putenv(), which won't work unless I have safe-mode enabled, which I'd prefer not to; and I also tried apache_setenv(), but that didn't seem to work either.

Are there any other solutions I might want to try?

(I am using a Linux machine with PHP 5.3.2)

like image 372
Frxstrem Avatar asked Feb 03 '23 02:02

Frxstrem


2 Answers

If you want to set it only in specific circumstances, you can do:

exec("PATH=/my/path ./bin");
like image 103
Artefacto Avatar answered Feb 05 '23 17:02

Artefacto


The way to alter the PATH used by apache on Mac OS X is described here: http://lists.apple.com/archives/macos-x-server/2008/Sep/msg00433.html

As stated in that post:

[A]dd the following text into [the file /System/Library/LaunchDaemons/org.apache.httpd.plist] at the fifth line:

<key>EnvironmentVariables</key> 
<dict>
<key>PATH</key>
<string>/usr/bin:/bin:/usr/sbin:/sbin:/opt/local/bin</ string>
</dict>

See the man page for launchd.plist(5) for details on the syntax I'm using here.

If you need to run your PHP commands as CLI sessions, you'll also probably need to add /opt/local/bin as a new path under /etc/paths.d work. For instance, something like this:

shell> sudo echo "/opt/local/bin" >> /etc/paths.d/macports

See the man page for path_helper(8).

like image 35
user908730 Avatar answered Feb 05 '23 15:02

user908730