OS X 中的 PHP 系统调用和 $PATH

发布于 2024-09-13 11:35:54 字数 314 浏览 11 评论 0原文

我试图让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

川水往事 2024-09-20 11:35:54

Mac OS X 上的环境变量是通过不同的机制设置的,具体取决于代码或其父进程的启动方式。为了确保从交互式 shell 启动的项目和 WindowServer 启动的项目具有相同的路径,您需要使 ~/.MacOSX/environment.plist 与 .profile(或 .cshrc)中的设置保持同步。

无需编辑环境即可实现目标的最简单方法是为通过系统命令执行的内容指定临时路径。例如:

char cmdStr[512] = "PATH=$PATH:/usr/local/bin";
strncat(cmdStr,"我的命令", 9);
系统(cmdStr);

这样,环境仅受到 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.:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文