Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php read file contents of network share file

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?

$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...

like image 771
sadmicrowave Avatar asked Feb 21 '11 19:02

sadmicrowave


2 Answers

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:

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:

$data = file_get_contents('/path/to/mount/path/to/file.txt');
like image 176
ircmaxell Avatar answered Sep 18 '22 01:09

ircmaxell


According to PHP Filesystem manual UNC/SMB files should be accessible. Try the following:

\\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.

like image 35
Brad Christie Avatar answered Sep 18 '22 01:09

Brad Christie