Read file on a network drive

I am running Xampp on Windows Server; Apache works as a service with a local account. On this server, the network resource is mounted as X: with specific credentials.

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

<?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 start Apache, not as a service, but directly by running httpd.exe ... and the code worked.

I don’t see what causes the difference between the service and the application and how to make it work.

+7
source share
2 answers

You cannot do this using the drive letter, since network drives with a network connection are designed for only one user and therefore cannot be used by services (even if you must install it for that user).

Instead, you can use the UNC path, for example:

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

Note, however, that there are several issues with accessing the PHP file system for UNC paths. One example is the error I filed for imagettftext , but there are also problems with file_exists and is_writeable . I did not report this to the latter, because, as you can see from my long-standing error with imagettftext , which point.

+11
source

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

If you use an IP address or host name, it should work fine:

 $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 
+3
source

All Articles