PHP无法打开网络文件 [英] PHP Can't Open Network File

查看:89
本文介绍了PHP无法打开网络文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站使用IIS在Windows Server 2012 R2上运行的PHP脚本.我正在尝试使用fopen来读取我们网络中另一台服务器上的文件,但是遇到了问题.我可以使用fopen命令在本地服务器上打开文件,并且可以使用PHP CLI在远程服务器上成功打开文件.

但是,当我尝试使用浏览器通过网站处理它时,它失败并显示以下错误:

无法打开流:权限被拒绝"

在IIS中,我创建了一个新的应用程序池,并将其标识设置为对两个服务器都具有管理员访问权限的域管理员.与运行CLI时使用的帐户相同.

这是我正在使用的PHP脚本代码:

fopen("//remoteservername/c$/inetpub/wwwroot/test.html", "w") or die("Can't open file!");

我已经尝试过使用反斜杠(也可以使用转义符).我试过在路径前面使用FILE://.我尝试将远程服务器的驱动器映射到运行PHP的Web服务器,并使用Y:\ inetpub之类的路径...由于它们都是Windows服务器,因此我尝试使用"rb"而不是"w".

我觉得它与网站正在运行的帐户/身份有关,但是我不确定为什么更改后它仍然无法正常工作.

更新:我提到fopen可以读取本地服务器上的文件,但似乎只有在使用如下所示的相对路径时,这才是正确的...

fopen("test.html", "w") or die("Can't open file!");

如果我尝试使用如下所示的绝对路径在本地服务器上打开同一文件,则它将失败.所以我不再相信这是一个身份问题...

fopen("//localservername/c$/inetpub/wwwroot/test.html", "w") or die("Can't open file!");

更新:所以我在CLI和浏览器上都运行了shell_exec("whoami").当我在CLI中执行此操作时,它将显示我正在运行Shell的域帐户.但是,当我通过浏览器运行脚本时,它显示"nt Authority \ iusr".有什么想法如何使它在其他用户下运行吗?更改IIS中的应用程序池标识似乎与我在测试中看到的没什么不同.

echo shell_exec("whoami");

解决方案

好,我能够解决此问题.我必须进入IIS> FastCGI设置>选择我的PHP应用程序>单击右侧栏上的编辑>展开高级菜单>将协议从NamedPipe更改为Tcp.

我遇到的方法是,我在另一个运行PHP的Windows Server上进行了测试,它使用的是AppPool中设置的正确帐户/身份.所以我在服务器上设置了一个新站点进行测试,甚至无法运行 回声shell_exec("whoami");当我开始对此进行故障排除时,我遇到了以下站点: http://tech.trailmax.info/2012/12/php-warning-shell_exec-unable-to-execute-on-iis-7/

我遵循了这些步骤,不仅解决了我无法运行shell_exec的问题,而且还开始使用在IIS AppPool中设置的域帐户开始运行我的PHP脚本.现在,我的fopen可以正常工作了.

I have a site using PHP scripts running on Windows Server 2012 R2 in IIS. I am trying to use fopen to read a file on another server in our network but am having problems. I am able to use the fopen command to open files on the local server and using the PHP CLI, I can successfully open the file on the remote server.

However, when I try to process it via the site using my browser, it fails with the error:

"failed to open stream: Permission denied"

In IIS, I created a new Application Pool and set its identity as a Domain Administrator which has admin access to both servers but this didn't seem to help. This is the same account that I ran the CLI with when it worked.

Here is my PHP script code that I'm using:

fopen("//remoteservername/c$/inetpub/wwwroot/test.html", "w") or die("Can't open file!");

I've tried using backslashes (with escaping ones too). I've tried using FILE:// ahead of the path. I've tried mapping the remote server's drive to the web server running PHP and using a path like Y:\inetpub...I've tried using "rb" instead of "w" since they're both Windows servers.

I feel like it has to do with the account/identity that the site is running under but I'm not sure why it still wouldn't be working after I changed it.

UPDATE: I mentioned that fopen works reading files on the local server but it seems that's only true if I use a relative path like below...

fopen("test.html", "w") or die("Can't open file!");

If I try opening that same file on the local server using an absolute path like shown below, it fails. So I'm not convinced anymore that it's an identity issue...

fopen("//localservername/c$/inetpub/wwwroot/test.html", "w") or die("Can't open file!");

UPDATE: So I ran shell_exec("whoami") at both the CLI and via browser. When I do it in the CLI, it shows my domain account that I'm running the shell at. However, when I run the script via browser, it shows "nt authority\iusr". Any ideas how to get that to run under a different user? Changing the Application Pool Identity within IIS does not seem to make a difference from what I can tell on my tests.

echo shell_exec("whoami");

解决方案

OK I was able to get this resolved. I had to go into IIS > FastCGI Settings > Select my PHP app > Click Edit on right sidebar > Expand Advanced menu > Change Protocol from NamedPipe to Tcp.

The way I came across this was I did a test on another Windows Server we have running PHP and it was using the correct account/identity that was set in the AppPool. So I setup a new site on my server to test with and I couldn't even run echo shell_exec("whoami"); When I began troubleshooting that, I came across this site: http://tech.trailmax.info/2012/12/php-warning-shell_exec-unable-to-execute-on-iis-7/

I followed those steps and not only did that fix my problem not being able to run shell_exec but also started running my PHP scripts using the domain account I had set in the IIS AppPool. Now my fopen works as expected.

这篇关于PHP无法打开网络文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆