Fixing stuff and maybe other things.

Access a mapped network drive from PHP (Windows)

So you want to do something on a mapped network drive using PHP, but it simply tells you it couldn’t find the drive? Worry not.

What’s going wrong?

The problem is that PHP runs under the SYSTEM account, and the SYSTEM account can’t access mapped drives.

The solution:

So, we need to map the drive from the SYSTEM account. You can do this from your PHP script, but if you need persistent access, we can do this:

1. Download the Windows Sysinternals Suite and unzip it somewhere you can easily get to from a command prompt.

2. Open an elevated command prompt. (Search for “cmd.exe” from the Start ball, and then right-click on it, and choose Run as Administrator.)

3. Using the command prompt, navigate to wherever you put Sysinternals.

4. Elevate yourself once again to supreme power by using:

psexec -i -s cmd.exe

A new command prompt window will open that is running as the SYSTEM account.

5. Map the network drive:

net use z: \\[IP ADDRESS HERE]\[FOLDER NAME HERE] /persistent:yes

And you’re done!


You can only remove this mapping the same way you created it, from the SYSTEM account. If you need to remove it, follow steps 1 -4 but change the command on step 5 to: net use z: /delete.

The newly created mapped drive will now appear for ALL users of this system but they will see it displayed as “Disconnected Network Drive (W:)”. Don’t worry though! It displays as disconnected, but will work for any user.

Posted under: Fixing Stuff, Web Design


#1 by Josh on 01.09.15 at 12:05 pm

This was perfect. Exactly what I was looking for and it worked as advertised.

#2 by Lavanya on 02.17.15 at 6:04 pm

Thanks for the useful post. But it didn’t really work for me due to some reason. I was able to map the drive by following the steps but PHP still gives me a “Permission denied” error. I tried to remove the mapping to try again but it didn’t work either. It says the network name cannot be found. It is much appreciated if you could help me with this.

Thank you

#3 by John on 03.23.15 at 3:09 am

Great man!
It worked perfectly, not just with PHP, but also with the virtual hosts I had configured in Apache.
Thank you very much indeed!

#4 by Wreckz on 04.06.15 at 1:22 am

saved my life!!!!!
thank you!

#5 by Dima on 08.06.15 at 3:41 pm

Thank you!!!

#6 by Konstantin on 08.23.15 at 11:54 pm

Awesome stuff, you just saved my life.

Leave a Comment