循环子文件夹,并在php中使它们成为动态链接

时间:2014-05-27 09:55:47

标签: php

我需要帮助 我已经设法循环我的父文件夹并回显想要在PHP文件上创建的子文件夹,它可以帮助我遍历子文件夹。我的代码如下:

<?php
$path="../downloads/pastpapers/UCU/foundations/";
$dir = new DirectoryIterator($path);
foreach ($dir as $fileinfo) {
    if ($fileinfo->isDir() && !$fileinfo->isDot()) {
        $dirName=$fileinfo->getFilename();
        echo "<div id='linkFrame'><a href='$dirName.php'><img src='images/folder.png'><br/>$dirName</img></a>   </div>";
    }
}
?>

1 个答案:

答案 0 :(得分:0)

只需在测试文件php中尝试以下内容:

<?php
$path = '../downloads/pastpapers/UCU/foundations';
ListFolder($path);

    function ListFolder($path)
    {
        //using the opendir function
        $dir_handle = @opendir($path) or die("Unable to open $path");

        //Leave only the lastest folder name
        $explode = explode("/", $path);
        $dirname = end($explode);

        //display the target folder.
        echo ("<li>$dirname\n");
        echo "<ul>\n";
        while (false !== ($file = readdir($dir_handle))) 
        {
            if($file!="." && $file!="..")
            {
                if (is_dir($path."/".$file))
                {
                    //Display a list of sub folders.
                    ListFolder($path."/".$file);
                }
                else
                {
                    //Display a list of files.
                    echo "<li>$file</li>";
                }
            }
        }
        echo "</ul>\n";
        echo "</li>\n";

        //closing the directory
        closedir($dir_handle);
    }
?>

有关详细信息,请访问this page