List all files in a directory// Note that !== did not exist until 4.0.0-RC2
<?php
if ($handle = opendir('/path/to/files')) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) { 
        echo "$file\n";
    }    /* This is the WRONG way to loop over the directory. */
    while ($file = readdir($handle)) { 
        echo "$file\n";
    }    closedir($handle); 
}
?>
 

解决方案 »

  1.   

    <?
    $d = dir("/etc");
    echo "Handle: ".$d->handle."<br>\n";
    echo "Path: ".$d->path."<br>\n";
    $entry=$d->read(); //如果不想列出当前目录(即.),读一次
    $entry=$d->read(); //如果不想列出上级目录(即..),读两次
    while($entry=$d->read()) {
        echo $entry."<br>\n";
    }
    $d->close();
    ?>
      

  2.   

    <?
    $FileCount = 0; //文件个数
    $DirectoryCount = 0; //目录个数
    function FCount($Path)
    {
    global $FileCount,$DirectoryCount;
        $Handle = opendir($Path);
        while($File = readdir($Handle))
        {
            if(filetype($Path.$File) != 'dir')
            {
                echo "----文件名----$File<br>";
                $FileCount++;
            }
            if($File !='.' && $File !='..' && filetype($Path.$File)=='dir')
            {
                echo "目录名----$File<br>";
                $DirectoryCount++;
                FCount($Path."$File/");
            }
        }
        closedir($Handle);
    }FCount('./hyforum_code/');
    echo "<br>请您注意脚本程序30秒钟超时警告<br><br>目录个数……………………$DirectoryCount<br>----文件个数……………………$FileCount<br>";
    ?>
      

  3.   

    改改手册上的例子:)<?php
    $d = dir("/etc");
    while (false !== ($entry = $d->read())) {
        if(is_dir($entry)){
           continue;
        }else{
           echo $entry."<br>\n";
        }
    }
    $d->close();
    ?>