问题描述
我正在尝试让PHP在OS X上进行系统调用.但是,它似乎无法找到系统路径中包含的任何内容.
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.
我跑步时...
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与我登录时使用的用户身份相同,因此从理论上讲,它可以访问与我相同的命令.
Apache2 is running as the same user as I'm logged in, so theoretically it has access to the same commands as me.
此外,相同的代码在我的Ubuntu机器上也可以正常工作.
Also, the same code works fine on my Ubuntu machine.
推荐答案
Mac OS X上的环境变量由不同的机制来设置,具体取决于启动代码或其父进程的方式.为了确保从交互式外壳程序启动的项目和WindowServer启动的项目具有相同的路径,您需要使〜/.MacOSX/environment.plist与.profile(或.cshrc)中设置的内容保持同步.
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);
这样,环境仅在MyCommand上下文中受到影响,然后再恢复.
This way the environment is only affected for the context of MyCommand and restored afterwards.
这篇关于PHP X系统调用和OS X中的$ PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!