PHP的exec()无法访问网络驱动器
在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我本来想说“你也不能从命令行做到这一点”,我确信这曾经是真的,但我刚刚在 WinXP Pro SP3 上尝试过,它正在工作,只是为了惹恼我。
前段时间我必须让 PHP 与网络驱动器通信(当时我在 PHP 世界里显然还比较绿),并且我做了一场噩梦让它工作,但最终我通过执行以下操作设法让它工作:
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()
的结果中收到错误消息。所以你的行看起来像:
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:
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 usesystem()
instead ofexec()
orshell_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()
andfopen()
, rather than trying toexec()
an external program against it.If you want to view the error messages from your call to
dir
, append2>&1
to the end of the command. This will redirect STDERR to STDOUT, so you should get the error messages in the result ofexec()
.So your line would look like: