php php读取网络共享文件的文件内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5070545/
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
Php read file contents of network share file
提问by sadmicrowave
I'm looking for a way to read the file contents of a file located on a network share. I can use the IP Address of the share host and the share folder to get to the location but I don't know the correct command and syntax to do that file_get_contents? fopen?
我正在寻找一种方法来读取位于网络共享上的文件的文件内容。我可以使用共享主机的 IP 地址和共享文件夹到达该位置,但我不知道执行该文件的正确命令和语法?开?
$text = fopen('//128.251.xxx.xxx/Common/sample.txt', 'r');
or something like that?
或类似的东西?
UPDATE* I also cannot access the file through a browser window although I know the file is located in that exact directory...
更新*我也无法通过浏览器窗口访问该文件,尽管我知道该文件位于该确切目录中...
回答by ircmaxell
The best way (depending on your needs) is to simply mount it on your local machine, and then read from the mount. That lets all the network interaction be abstracted away.
最好的方法(取决于您的需要)是简单地将它挂载到您的本地机器上,然后从挂载中读取。这让所有的网络交互都被抽象掉了。
So, in Linux:
所以,在 Linux 中:
mount -t cifs //192.168.XXX.XXX/share /path/to/mount -o user=username,password=password
Then, in php, just access it from the mount point:
然后,在 php 中,只需从挂载点访问它:
$data = file_get_contents('/path/to/mount/path/to/file.txt');
回答by Brad Christie
According to PHP Filesystem manualUNC/SMB files should be accessible. Try the following:
根据PHP 文件系统手册,应该可以访问 UNC/SMB 文件。请尝试以下操作:
\server\share\file.txt
It may be one of those cases where the //
may be mistook as a reference to the root directory. And given this is an SMB path, I would use the traditional backslashes.
它可能是那些//
可能被误认为是对根目录的引用的情况之一。鉴于这是一个 SMB 路径,我将使用传统的反斜杠。