3

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?

Gary Willoughby
  • 46,098
  • 37
  • 127
  • 193
  • 1
    Could you side-step the problem by mapping it to a drive? – Greg Aug 16 '09 at 17:50
  • Also are you using PHP or Apache? If you google "php unc path" you get some results about permissions that may help you, but they differ between web servers – Greg Aug 16 '09 at 17:57
  • @Greg: it's both, surely? He's using the PHP language on the Apache server on Windows (I assume). At least, the two are not mutually exclusive. – DisgruntledGoat Aug 16 '09 at 18:09

3 Answers3

4

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
  • 62,693
  • 62
  • 192
  • 281
1

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
  • 46,098
  • 37
  • 127
  • 193
0

The following process works for me. Just mapping the shared location to our system.
If you have Windows 7: Click Start, then Computer,Click Map Network Drive
If you have Windows 8: Open your File Explorer Click the "Computer" Tab, then "Map Network Drive". By mentioning shared drive/folder we can access the contents directly.

SivaJyothiG
  • 87
  • 10