Reading file list from a mapped Windows network drive
Posted by Stanislav Furman
Last week I ran into a situation when I had to read directory file list from a mapped Windows network drive. At your very first look it may seem pretty simple. It is! However, there is a little trick - Windows may require your network/domain credentials, and then standard PHP functions such as opendir() won't be able to access the directory. Fortunately, the solution is pretty simple:
<?php
$path = '\\networkdrive\somepath\your_directory';
$user = "username";
$pass = "password";
$drive_letter = "Q";
system("net use ".$drive_letter.": \"".$path."\" ".$pass." /user:".$user." /persistent:no>nul 2>&1");
$location = $drive_letter.":/somepath/your_directory";
if ($handle = opendir($location)) {
while (false !== ($entry = readdir($handle))) {
echo "$entry";
}
closedir($handle);
}
?>
Update: If you run the script on Windows localhost, you will need to run your localhost Apache as administrator. If this doesn't work, try to restart Apache service from Windows Administrator tools.
Update 2: File search and wildcards in PHP.
Comments
I've updated the code.
what you mean by somepath , your_directory and drive_letter
Leave your comment