如何使用PHP在本地网络上遍历目录?

时间:2009-08-16 17:44:32

标签: php directory-walk

如何使用PHP列出Windows共享的内容?

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

打印(执行opendir($ SearchFolder的));给出了这个错误:

  

警告:   执行opendir(\ 192.168.1.100 \ pdfoutput)   [function.opendir]:无法打开   迪尔:没有错误   C:\ Users \用户加里\ Web服务器\ QuickMail \ maildetails.php   在第227行

这不按预期工作。有什么想法吗?

3 个答案:

答案 0 :(得分:3)

http://uk3.php.net/function.opendir查看opendir函数的用户注释。看起来可能会有一些信息对您有所帮助。具体来说,DaveRandom的这段代码可以解决您的问题:

<?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")
?>

答案 1 :(得分:1)

我找到了使用本地网络路径和使用FTP服务器的好方法。考虑到我需要从这个目录中显示一些图像,这也很有效。我使用的FTP服务器很轻,允许从整个局域网访问这个目录,没有任何安全或权限错误。

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

答案 2 :(得分:0)

以下过程适用于我。 只需将共享位置映射到我们的系统。
如果您使用的是Windows 7:依次单击“开始”,“计算机”和“映射网络驱动器”
如果您使用Windows 8:打开文件资源管理器单击“计算机”选项卡,然后单击“映射网络驱动器”。通过提及共享驱动器/文件夹,我们可以直接访问内容。