How to walk a directory over a local network using PHP?

Gary Willoughby picture Gary Willoughby · Aug 16, 2009 · Viewed 8.9k times · Source

How can i list the contents of a windows share using PHP?

$SearchFolder = "\\\\192.168.1.100\\pdfoutput\\";

if (is_dir($SearchFolder))
{
    if ($Directory = opendir($SearchFolder))
    {
        while (($File = readdir($Directory)) !== false)
        {
            if(filetype($SearchFolder.$File) == "file")
            {
                $this->Attachments[] = new Attachment($SearchFolder.$File);
            }
        }
        closedir($Directory);
    }
}

Print(opendir($SearchFolder)); gives this error:

Warning: opendir(\192.168.1.100\pdfoutput) [function.opendir]: failed to open dir: No error in C:\Users\gary\Webserver\QuickMail\maildetails.php on line 227

This is not working as expected. Any thoughts?

Answer

DisgruntledGoat picture DisgruntledGoat · Aug 16, 2009

Take a look at the user comments for the opendir function at http://uk3.php.net/function.opendir . It looks like there may be some information that will help you. Specifically, this bit of code by DaveRandom may solve your problem:

<?php
// Define the parameters for the shell command
$location = "\\servername\sharename";
$user = "USERNAME";
$pass = "PASSWORD";
$letter = "Z";

// Map the drive
system("net use ".$letter.": \"".$location."\" ".$pass." /user:".$user." /persistent:no>nul 2>&1");

// Open the directory
$dir = opendir($letter.":/an/example/path")
?>