简体   繁体   中英

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)

如果只想在特定情况下进行设置,可以执行以下操作:

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

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

而不是设置bin的路径并调用foo ,为什么不显式调用bin/foo

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM