...

View Full Version : Accessing files on network server with PHP



oldhack
09-21-2005, 10:38 PM
How can I access files on a remote/network file server using PHP?

I'm running an Apache server (v2.0.54) and PHP (v5.0.4) on a Win2K machine.

I need to see if a file exists, but using file_exists('\\\Server\Share\Dir\test.txt'); always returns false for a file that does exist.

I've also tried using the mapped letter in the path to no avail (e.g. 'G:\Dir\test.txt' - where "G:" is mapped to the "Share" dir on "Server").

I added the full path and the mapped drive letter to the "include_path" line in the php.ini file as well but still nothing.

I know for security reasons that this can be dangerous but this is only accessable by internal users so the security is not a big deal.

The error messages I get in the Apache log are:
PHP Warning: main() [<a href='function.include'>function.include</a>]: Failed opening 'test.txt' for inclusion (include_path='.;c:\\php\\includes;\\\\Server\\Share\\Dir\\;L:;L:\\') in C:\\Server\\Apache2\\htdocs\\Test\\remoteTest.php on line 14


Help!!!

JamieR
09-21-2005, 10:41 PM
I need to see if a file exists, but using file_exists('\\\Server\Share\Dir\test.txt');

Just a long shot but why are you using \\\Server? That's not a windows network call path...\\Server\Share\Dir\test.txt however is (two \\ at the beginning rather than three)

oldhack
09-22-2005, 02:22 PM
Just a long shot but why are you using \\\Server? That's not a windows network call path...\\Server\Share\Dir\test.txt however is (two \\ at the beginning rather than three)

The "\" is the escape char in PHP so, if you only use two you really only get one in the string.

Also, I actually escape the "\t" in the "\test.txt" by "\\test.txt" because the "\t" is interpreted as a (tab) I believe.

I use echo $path; in the script prior to calling file_exists just to make sure I am getting a valid path (that's how I found that "\t" issue).

Fou-Lu
09-23-2005, 09:42 AM
The "\" is the escape char in PHP so, if you only use two you really only get one in the string.

Also, I actually escape the "\t" in the "\test.txt" by "\\test.txt" because the "\t" is interpreted as a (tab) I believe.

I use echo $path; in the script prior to calling file_exists just to make sure I am getting a valid path (that's how I found that "\t" issue).

This technique only applies where php is being evaluated using regexp.
The line: file_exists('\\\Server\Share\Dir\test.txt');, the arguments are not being evaluated as php code, they are being evaluated as string data.
However, you may have more problems with your include_path directive.

include_path = ".;C:\Path\to\php\includes;L:\";

This value can be set via ini, htaccess, httpd.conf or windows registry.
This assumes that 'L' drive if your network shared files. Remember as well that with this code it will evalute from left to right, attempt to find file located in C:\Path\to\root\Server\Share\Dir\test.txt first from your document root, then from your C:\path\to\php\includes, and then from your L drive: L:\Server\Share\Dir\test.txt. Give that alteration a try, I haven't set up php to look across the network, but I see no reason why it shouldn't work.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum