PHP的exec()无法访问网络驱动器

发布于 2024-12-21 10:50:01 字数 215 浏览 0 评论 0原文

在 PHP 中,我通过调用 exec("dir ...") 列出一些文件。然而,奇怪的是,这仅适用于本地驱动器。在网络驱动器上,它具有非零结果状态代码,并且不返回任何结果。

我在 Windows XP Professional 上运行 Apache。

有什么技巧可以解决这个问题吗?或者查看错误消息?

编辑:apache 与我在同一用户下运行,我可以从命令行执行此操作

In PHP, I list some file by calling exec("dir ..."). However, this strangely works only on local drives. On network drives it has non-zero result status code and no results are returned.

I run Apache on Windows XP Professional.

Is there any trick to fix this? Or to view an error message?

EDIT: apache is running under the same user as I am and I can do it from the command line

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

你又不是我 2024-12-28 10:50:01

我本来想说“你也不能从命令行做到这一点”,我确信这曾经是真的,但我刚刚在 WinXP Pro SP3 上尝试过,它正在工作,只是为了惹恼我。

前段时间我必须让 PHP 与网络驱动器通信(当时我在 PHP 世界里显然还比较绿),并且我做了一场噩梦让它工作,但最终我通过执行以下操作设法让它工作:

  • 在“服务”MMC 管理单元中,将运行 Apache 的用户帐户更改为本地用户。目前它可能会设置为 SYSTEM 并且(据我所知)该帐户无权访问网络驱动器。执行此操作后,您将需要重新启动该服务。
  • 在尝试访问之前添加以下行:system('net use Z: "\\servername\sharename" PASSWORD /user:USERNAME /persistent:no');,在其中更改驱动器字母、UNC 路径、用户名等以满足您的要求。即使驱动器已映射,也要执行此操作。我似乎记得我必须使用 system() 而不是 exec()shell_exec() ,否则它不起作用 - 因为结果,您需要输出缓冲区来停止将输出传递到 STDOUT。

我不知道为什么会这样,但确实如此。但请注意,我尝试将驱动器与本机 PHP 函数(例如 opendir()fopen())一起使用,而不是尝试 exec()< /code> 一个针对它的外部程序。

如果您想查看调用 dir 时的错误消息,请将 2>&1 附加到命令末尾。这会将 STDERR 重定向到 STDOUT,因此您应该在 exec() 的结果中收到错误消息。

所以你的行看起来像:

exec("dir Z:\\some\\path 2>&1")

I was going to say "you can't do it from the command line either", and I'm sure that used to be true, but I have just tried on WinXP Pro SP3 and it is working, just to spite me.

I had to get PHP to talk to a network drive some time ago (when I was decidedly greener in the world of PHP), and I had a nightmare getting it to work, however eventually I managed to get it to work by doing the following:

  • In the "Services" MMC snap-in, change the user account that Apache runs under to a local user. It will probably be set to SYSTEM at the moment and (AFAIK) that account does not have access to network drives. You will need to restart the service after you do this.
  • Add the following line before you try and access it: system('net use Z: "\\servername\sharename" PASSWORD /user:USERNAME /persistent:no');, where you change the drive letter, UNC path, username etc to match your requirements. Do this even if the drive is already mapped. I seem to remember that I had to use system() instead of exec() or shell_exec() or it didn't work - as a result, you need to output-buffer to stop the output being passed to STDOUT.

I have no idea why this worked, but it did. Note, though, that I was trying to use the drive with native PHP functions like opendir() and fopen(), rather than trying to exec() an external program against it.

If you want to view the error messages from your call to dir, append 2>&1 to the end of the command. This will redirect STDERR to STDOUT, so you should get the error messages in the result of exec().

So your line would look like:

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