Freyja
Freyja

Reputation: 40784

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)

Upvotes: 9

Views: 15624

Answers (4)

user908730
user908730

Reputation: 41

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

Upvotes: 4

Artefacto
Artefacto

Reputation: 97805

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

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

Upvotes: 6

canni
canni

Reputation: 5885

If You have path set for Yours user AND if Your scripts run as Yours user, only thing You should do, is to set up this path for Yours shell, but is Your's scripts run as ie. apache user (www-data in debian-like systems) for this to work, You should set this PATH for that user explicte

Upvotes: 0

muhmuhten
muhmuhten

Reputation: 3341

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

Upvotes: 2

Related Questions