3

I have mapped a network drive to a computer in my home network. Now I am trying to access it via PHP - I did this quick test:

echo opendir('Z:\\');

This gives me:

Warning: opendir(Z:\) [function.opendir]: failed to open dir: No error in C:\wamp\www\webs\tester-function.php  on line 3

What have I done wrong here?

I don't want my users typing in the UNC path so is there a way to get the UNC path for them and maybe that will work when I try to access it? This is possible in Microsoft languages but I am not sure how to get PHP to do this - maybe using a cmd.exe command?

Please note, the mapped drive does exist as I can see it and I can access it. It also does not appear to be a permissions problem as I am assuming it would of complained about this IF it could access that drive...right?

Thanks all for any help

Kate Gregory
  • 18,808
  • 8
  • 56
  • 85
Abs
  • 56,052
  • 101
  • 275
  • 409
  • *(related)* http://bugs.php.net/40117 – Gordon Apr 07 '10 at 14:05
  • This script is executed by a php instance that was created by a webserver? Is this webserver running as a service? Is this service running as `localsystem` or do you use another account? For which account do you create the drive map (your own or for the service's account)? – VolkerK Apr 07 '10 at 14:26
  • Yes, a php instance created by the web-server. Yes, the web-server is running as a service. Yes as `localsystem`. This mapped drive is created by me not the `localsystem`. However, the `localsystem` has permissions set for this mapped drive automatically. – Abs Apr 07 '10 at 14:37

3 Answers3

5

The User Contributed Notes to opendir() have this:

I was trying to access network drives using this opendir function. I read so many posts saying that it was almost impossible to access a network drive and finally, I found the answer; there are 2 steps to be followed to access a network drive with PHP either on the same machine or another machine.

in this case, the user rights seemed indeed to be the problem, which can be different from your rights depending on what user PHP / the web server are running on.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • That's interesting! Apache runs as the user 'SYSTEM' - in the permissions settings - the mapped drive has the user 'SYSTEM' to be able to read the drive. In this way, I have done those two steps and it still doesn't work. Maybe I misunderstood something?! – Abs Apr 07 '10 at 14:25
  • How did you create the drive map? Creating the drive map for your account doesn't mean the webserver's process/account gets the same mapping. – VolkerK Apr 07 '10 at 14:28
  • @Abs hard to tell. Maybe try to run the service under a specific user account as outlined in the linked comment, just to see whether it works that way? – Pekka Apr 07 '10 at 14:29
  • I will try this. @VolkerK - I used the normal map drive network drive wizard logged in as me "Abs-PC". Once I did this, there were other users and user-groups that got permissions set (all to at least read) one of which is SYSTEM, which the httpd service happens to run under. – Abs Apr 07 '10 at 14:33
  • @Abs: Take a look at http://msdn.microsoft.com/en-us/library/aa363908%28v=VS.85%29.aspx . You've created a network drive in the _local_ MS-DOS device namespace. – VolkerK Apr 07 '10 at 14:43
4

Open the Services MMC by going to Start -> Run (or by using the search box, if using Windows 7) and typing Services.msc and pressing enter. Find the Apache service. Having never used WAMP, I have no idea what it's been named. Hopefully it starts with the word "Apache" though, and thus shouldn't be too hard to find. Right-click on the service and select Properties. Switch to the Log On tab. Select the radio button next to "This account:" and enter in the credentials for the user account you want to run Apache under. If it's a network/domain account, use the "DOMAIN\user" syntax (or search for the account using the Browse button). Select OK and restart the Apache service. Verify that your changes worked by looking for the "httpd.exe" process in Task Manager and checking to see which user name the process is running under.

reference http://board.phpbuilder.com/board/showthread.php?t=10371870&page=2

Ali Pourheidar
  • 107
  • 1
  • 3
  • Be sure to understand the security implications of what you are doing by changing the user Apache is run under. Being the Local system account gives it limited access to important areas of the file structure and NO access to the network. By changing this you might be giving access to protected areas and any network access the user has. This might mean that once someone hacks your program they will have access to your entire computer and your network (to be able to place files). If possible try to find a way to do this within the confines of the security that is built into Apache and Windows. – Robert Jun 28 '15 at 21:15
3

simple, use: "\\\\server\\dir\\subfolder" as the path. make sure you can access the "server".

works for me.
$arquivo = "\\\\server\\dir\\subfolder\\file.txt"; echo is_file($arquivo); //print 1.

Guilherme
  • 31
  • 1
  • This worked great fro me, tried a lot of other ways before getting to this. No need to change any permissions for this, at least in my case. – user3101337 Dec 03 '14 at 01:44