OS X 中的 PHP 系统调用和 $PATH
我试图让 PHP 在 OS X 上进行系统调用。但是,它似乎无法找到系统路径中包含的任何内容。
当我运行...
putenv("PATH={$_SERVER["PATH"]}:/usr/local/bin");
...就在系统调用之前,它起作用了。这不是一个实用的解决方案,因为执行系统调用的代码是一个插件,所以我宁愿不接触源代码,这会使其在更新时不兼容。
Apache2 以与我登录的用户相同的用户身份运行,因此理论上它可以访问与我相同的命令。
另外,相同的代码在我的 Ubuntu 机器上运行良好。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Mac OS X 上的环境变量是通过不同的机制设置的,具体取决于代码或其父进程的启动方式。为了确保从交互式 shell 启动的项目和 WindowServer 启动的项目具有相同的路径,您需要使 ~/.MacOSX/environment.plist 与 .profile(或 .cshrc)中的设置保持同步。
无需编辑环境即可实现目标的最简单方法是为通过系统命令执行的内容指定临时路径。例如:
这样,环境仅受到 MyCommand 上下文的影响,并在之后恢复。
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.:
This way the environment is only affected for the context of MyCommand and restored afterwards.