How to set $PATH in PHP?

13,039

Solution 1

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

exec("PATH=/my/path ./bin");

Solution 2

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).

Solution 3

instead of setting the path to bin and calling foo, why don't you just explicitly invoke bin/foo?

Share:
13,039
Frxstrem
Author by

Frxstrem

Updated on July 25, 2022

Comments

  • Frxstrem
    Frxstrem almost 2 years

    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)

  • Frxstrem
    Frxstrem over 13 years
    The problem is that I do not want it to apply everywhere on the server, only in one PHP script...
  • canni
    canni over 13 years
    Than, You cannot do that without disabling safe-mode (as my knowledge says, but i may be wrong), PATH is a shell-oriented environment variable, not a script-oriented
  • Frxstrem
    Frxstrem over 13 years
    Thanks, why didn't I think of that? :P
  • muhmuhten
    muhmuhten over 13 years
    that PATH setting is kind of pointless since you're also giving an actual relative path.
  • Artefacto
    Artefacto over 13 years
    @sreservoir No it's not. The path doesn't affect only the resolution of ./bin (in this case it doesn't affect it at all, since it's a relative path). If ./bin is e.g. a script that calls other applications it can affect the path resolution for them.