9

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.

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

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

2
  • windows network drivers are per-user. unless you've mapped that X: drive under the account that xampp's running, it won't be a usable drive.
    – Marc B
    Jan 16, 2013 at 21:01
  • Hi, thank you for your answer. I log on Windows as the user that run Xampp but mount x: with another account. Jan 17, 2013 at 10:01

3 Answers 3

16

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:

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_exists and is_writeable. I haven't reported the latter because as you can see from my long-outstanding bug with imagettftext, what's the point.

4
  • Hi, Thank you for the answer. When I use this syntax, I get "failed to open stream: Invalid argument". When I user scandir() I get "Logon failure: unknown user name or bad password. (code: 1326)". I guess that, as you said, service can't use an existing credential. Jan 17, 2013 at 10:07
  • @user1985014 It can access it, but you have to configure the service to use the correct account. I haven't worked with Apache on Windows for years, but IIS does this very easily per Application Pool. Jan 17, 2013 at 10:20
  • So If I can't run Apache as the user who has the rights to access the share, i'm stuck ? Jan 17, 2013 at 10:52
  • @user1985014 Correct. But there's no reason you can't change the user of the Apache service, or add a new user to access the share. Jan 17, 2013 at 11:09
7

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

If you use the IP or hostname 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
  • For me, none of those 3 examples above worked, in PHP 7.2, on Win10 (NTFS)
    – userfuser
    Dec 30, 2020 at 16:49
  • @userfuser thats ok, things may change in 8 years, when you find a solution you should post an answer to help others using PHP 7.2, on Win10 Dec 30, 2020 at 21:18
  • Of course, that's why I posted it in the first place - so that some people (for whom it is useful) would realize sooner that something actually changed in PHP or Win and it won't work for them.
    – userfuser
    May 13, 2021 at 14:20
3

If it helps for the future, what i did was this:

  1. Create a local account with the same username and password as the account which you access to the network drive.
  2. Go to Windows Services > Apache2 > Properties > Login > Log in as this account ... and specify the username and password that you created.

About the syntax, the one that worked for me was:

$fileName = '//192.168.1.10/Folder/file.txt';

I'm using php 8.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.