简体   繁体   English

PHP递归目录菜单(不同方法)

[英]PHP recursive directory menu (different approaches)

I'm trying to create a PHP menu which displays the directories and files in each directory in a list, with links to each file in those directories (but not links to the directories themselves). 我正在尝试创建一个PHP菜单,它在列表中的每个目录中显示目录和文件,并指向这些目录中的每个文件的链接(但不是指向目录本身的链接)。 The directories are numerical as years '2013', '2014', etc., and the files in these directories are PDFs. 这些目录的数字为“2013”​​,“2014”等年份,这些目录中的文件是PDF。

In essence: 在本质上:

--- 2013 (not linked) --- 2013(未链接)

--------------- 01.pdf (linked) --------------- 01.pdf(链接)

--------------- 02.pdf (linked) --------------- 02.pdf(链接)

--------------- 03.pdf (linked) --------------- 03.pdf(已链接)

--- 2014 (not linked) --- 2014(未链接)

--------------- 04.pdf (linked) --------------- 04.pdf(链接)

--------------- 05.pdf (linked) --------------- 05.pdf(链接)

--------------- 06.pdf (linked) --------------- 06.pdf(链接)

Currently, my code looks like this: 目前,我的代码如下所示:

<?php
function read_dir_content($parent_dir, $depth = 0){
    if ($handle = opendir($parent_dir)) 
    {
        while (false !== ($file = readdir($handle)))
        {
            if(in_array($file, array('.', '..'))) continue;
            if( is_dir($parent_dir . "/" . $file) ){
                $str_result .= "<li>" . read_dir_content($parent_dir . "/" . $file, $depth++) . "</li>";
            }
            $str_result .= "<li><a href='prs/{$file}'>{$file}</a></li>";
        }
        closedir($handle);
    }
    $str_result .= "</ul>";
    return $str_result;
}
echo "<ul>".read_dir_content("prs/")."</ul>";
?>

However, this creates a complete mess when processed. 但是,这会在处理时造成完全混乱。 (Unfortunately I cannot post an image as I am a new user, but if it's not too taboo, I will provide a sneaky link to it: http://i.stack.imgur.com/gmIFz.png ) (不幸的是我无法发布图像,因为我是新用户,但如果它不是太禁忌,我会提供一个偷偷摸摸的链接: http//i.stack.imgur.com/gmIFz.png

My questions/requests for help on: 我的问题/请求帮助:

1. Why is the order reversed alphanumerically, ie why is 2013 at the bottom of the list and 2014 at the top? 1.为什么订单以字母数字方式逆转,即为什么2013年位于列表底部而2014年位于顶部?

2. How can I remove the links for the directories while keeping the links for the PDFs? 2.如何在保留PDF链接的同时删除目录的链接?

3. I'm confused over why there are empty list items at the end of each directory's list and why they are not logically/consistently spaced, ie why are the 01, 02, 03 PDFs not subordinate to the 2013 (see 'in essence' spacing above)? 3.我很困惑为什么在每个目录列表的末尾都有空列表项以及为什么它们在逻辑上/一致地间隔,为什么01,02,03 PDF不是从属于2013(参见'实质上' '上面的间距)?

NB I am new to programming and PHP, so please bear in mind that my obvious mistake/s might be very simple. NB我是编程和PHP的新手,所以请记住,我明显的错误可能非常简单。 Sorry in advance if they are. 如果他们是,请提前抱歉。

edit : what would also be a massive bonus would be to get rid of the ".pdf"s on the end of the filenames, but that is probably an entirely different question/matter. 编辑 :什么也是一个巨大的奖金将摆脱文件名末尾的“.pdf”,但这可能是一个完全不同的问题/问题。

PHP5 has class called RecursiveDirectoryIterator PHP5有一个名为RecursiveDirectoryIterator的

This code : 这段代码:

  1. Sorts the list as you want (2013 first then 2014) 根据需要对列表进行排序(2013年首次至2014年)
  2. Does not show the links to the directories 不显示指向目录的链接
  3. Solves the formatting troubles 解决格式化问题
  4. BONUS: removes the pdf extension 奖金:删除pdf扩展名

.. ..

$myLinks = array();
$dirIterator = new RecursiveDirectoryIterator($parent_dir);

//iterates main prs directory
foreach ($dirIterator as $file) {

    // If its a directory, it will iterate it's children (except if it's . or ..)
    if ($file->isDir() && $file->getFilename() != '.' && $file->getFilename() != '..') {
        $dir = new RecursiveDirectoryIterator($file->getRealPath());
        $myLinks[$file->getFilename()] = array();
        foreach ($dir as $subFile) {
            //If it finds a file whose extension is pdf
            if ($subFile->isFile() && $subFile->getExtension() == 'pdf') {
                // Gets its filename and removes extension
                $fname = str_replace('.pdf', '', $subFile->getFilename());
                // adds the file information to an array
                $myLinks[$file->getFilename()][$fname] = "prs/{$file->getFilename()}/{$subFile->getFilename()}";
            }
        }
    }
}

//Sort our array alphabetically (and numerically) and recursively
ksort_recursive($myLinks);

function ksort_recursive(&$array)
{
    if (!is_array($array)) return;
    ksort($array);
    foreach ($array as $k=>$v) {
        ksort_recursive($array[$k]);
    }
}



// now we print our links as a unordered list
print '<ul>';
foreach ($myLinks as $year => $val) {
    print "<li>$year";
    foreach ($val as $name => $link) {
        print "<li><a href='$link'>$name</a></li>";
    }
    print '</li>';
}
print '</ul>';

First off I see this line in there 首先,我在那里看到这条线

$str_result .= "</ul>";

but I don't see an opening ul tag. 但我没有看到一个开放的ul标签。 This is probably the source of the crazy looking result. 这可能是疯狂结果的来源。

1) I would use scandir instead of readdir as it would let you set the sort order. 1)我会使用scandir而不是readdir,因为它可以让你设置排序顺序。

2,3) Get rid of $str_result .= "</ul>"; 2,3)摆脱$str_result .= "</ul>"; and try something like this in your inside your while loop to remove the dir links and get the order right: (NOTE: I have not run this) 并在你的while循环中尝试这样的东西来删除dir链接并获得正确的顺序:(注意:我没有运行这个)

if( ! in_array($file, array('.', '..')) ) { 
    if( is_dir($parent_dir . "/" . $file) ){
        $str_result .= "<li>{$file}</li>";
        $str_result .= "<ul>";
        $str_result .= "<li>" . read_dir_content($parent_dir . "/" . $file, $depth++) . "</li>";
        $str_result .= "</ul>";
    } else {
        $str_result .= "<li><a href='prs/{$file}'>{$file}</a></li>";
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM