PHP system calls and $PATH in OS X

折月煮酒 提交于 2019-12-31 06:07:57

问题


I'm trying to get PHP to make system calls on OS X. However, it doesn't seem to be able to find anything that's included in the system path.

When I run...

putenv("PATH={$_SERVER["PATH"]}:/usr/local/bin");

... just before the system call, it works. This is not a practical solution, since the code that executes the system call is a plugin, so I'd rather not touch source code that'll make it incompatible come an update.

Apache2 is running as the same user as I'm logged in, so theoretically it has access to the same commands as me.

Also, the same code works fine on my Ubuntu machine.


回答1:


Environment variables on Mac OS X are set by differing mechanisms depending on how your code, or its parent process, was launched. To insure that items launched from an interactive shell and items launched by the WindowServer have the same path, you need to keep ~/.MacOSX/environment.plist in sync with what is set in .profile (or .cshrc).

The simplest means of accomplishing your goal without having to resort to editing the environment would be to specify a temporary path for what you are executing via your system command. e.g.:

char cmdStr[512] = "PATH=$PATH:/usr/local/bin";
strncat(cmdStr,"MyCommand", 9);
system(cmdStr);

This way the environment is only affected for the context of MyCommand and restored afterwards.



来源:https://stackoverflow.com/questions/3431858/php-system-calls-and-path-in-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!