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.
Please correct this code... (1) $letter is not defined (2) $handle is not defined
Thanks, David! I've updated the code.
Worked perfectly! Been searching for an answer for this, thanks a ton!
Kindly give an example what you mean by somepath , your_directory and drive_letter
How to connect other network drive? give me a example