views:

275

answers:

2

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?

+1  A: 

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")
?>
DisgruntledGoat
A: 

I've found a good alternative to using local network paths and that is using an FTP server. This works great also considering i needed to display some images from this directory as well. The FTP server i've used is very light and allows access to this directory from the entire LAN without any security or permissions errors.

$SearchFolder = "ftp://192.168.0.104/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);
    }
}
Gary Willoughby