php 读取网络驱动器上的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14367687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:08:41  来源:igfitidea点击:

Read file on a network drive

phpnetworking

提问by user1985014

I'm running Xampp on a Windows Server ; Apache is running as a service with a local account. On this server, a network share is mounted as X: with specific credentials.

我在 Windows Server 上运行 Xampp;Apache 作为具有本地帐户的服务运行。在此服务器上,网络共享安装为 X: 具有特定凭据。

I want to access files located on X: and run the following code

我想访问位于 X: 上的文件并运行以下代码

<?php
echo shell_exec("whoami");
fopen('X:\text.txt',"r");
?>

and get

并得到

theservername\thelocaluser
Warning: fopen(X:\text.txt) [function.fopen]: failed to open stream: No such file or directory

I tried to run Apache, not as a service but directly by launching httpd.exe ... and the code worked.

我试图运行 Apache,而不是作为服务,而是直接通过启动 httpd.exe 来运行......并且代码有效。

I can't see what causes the difference between the service and the application and how to make it works.

我看不出是什么导致了服务和应用程序之间的差异以及如何使其工作。

回答by Rudi Visser

You're not able to do this using a drive letter, as network mapped drives are for a single user only and so can't be used by services (even if you were to mount it for that user).

您无法使用驱动器号来执行此操作,因为网络映射驱动器仅供单个用户使用,因此服务无法使用(即使您要为该用户安装它)。

What you can do instead is use the UNC path directly, for example:

您可以做的是直接使用 UNC 路径,例如:

fopen('\\server\share\text.txt', 'r');

Note, however, that there are a few issues with PHP's filesystem access for UNC paths. One example is a bug I filed for imagettftext, but there are also issues with file_existsand is_writeable. I haven't reported the latter because as you can see from my long-outstanding bug with imagettftext, what's the point.

但是请注意,PHP 对 UNC 路径的文件系统访问存在一些问题。一个例子是我为 imagettftext 提交错误,但也存在file_exists和 的问题is_writeable。我没有报告后者,因为正如你从我长期未解决的错误中看到的那样imagettftext,有什么意义。

回答by Lawrence Cherone

For network shares you should use UNC names: "//server/share/dir/file.ext"

对于网络共享,您应该使用 UNC 名称:“//server/share/dir/file.ext”

If you use the IP or hostname it should work fine:

如果您使用 IP 或主机名,它应该可以正常工作:

$isFolder = is_dir("\\NAS\Main Disk");
var_dump($isFolder); //TRUE

$isFolder = is_dir("//NAS/Main Disk");
var_dump($isFolder); //TRUE

$isFolder = is_dir("N:/Main Disk");
var_dump($isFolder); //FALSE