Search code examples
phpdirectory-walk

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


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?


Solution

  • 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);
        }
    }