每个文件夹中的目录和秀文件的文件夹的PHP数组 [英] PHP array of folders in directory and show files in each folder

查看:157
本文介绍了每个文件夹中的目录和秀文件的文件夹的PHP数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的code可以列出一个目录(称为测试)的所有文件夹,这些文件夹中的所有文件:

I am using the following code to list all folders in a directory (called test), and all files within those folders:

<?php
function listFolderFiles($dir){
$ffs = scandir($dir);
echo '<ol>';
foreach($ffs as $ff){
    if($ff != '.' && $ff != '..'){
        echo '<li class="title">'.$ff;
        if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
        echo '</li>';
    }
}
echo '</ol>';
}

listFolderFiles('test');

?>

这工作得很好,但是我希望能够在这些文件夹链接到每个文件。谁能告诉我,我会怎么做呢?

This works fine, however I want to be able to link to each file in those folders. Can anyone tell me how I would do this?

例如我有一个名为测试,在子目录测试1,测试2和测试3目录。在每个那些我有一对夫妇,我想成为一个用户可以单击显示文件的链接文件。因此,当一个用户进入该网站,他们将看到这样的事情:

For example I have a directory called "test" with subdirectories "test 1", "test 2" and "test 3". In each of those I have a couple of files that I would like to be links that a user can click on to show the file. So when a user goes to the site they will see something like this:

测试1:
链接1
链接2

Test 1: link 1 link 2

测试2:
链接1
链接2

Test 2: link 1 link 2

测试3:
链接1
链接2

Test 3: link 1 link 2

推荐答案

只需添加一个HTML的标记的李标记内你是输出。沿着这些线路的东西应该工作:

Just add a html A tag inside the LI tag you are outputting. Something along these lines should work:

<?php
function listFolderFiles($dir){
    $ffs = scandir($dir);
    echo '<ol>';
    foreach($ffs as $ff){
        if($ff != '.' && $ff != '..'){
            echo '<li class="title">';
            if(is_dir($dir.'/'.$ff)){
                echo $ff;
                listFolderFiles($dir.'/'.$ff);
            }else{
                echo '<a href="'.$dir.'/'.$ff.'">'.$ff.'</a>';
            }
            echo '</li>';
        }
    }
    echo '</ol>';
}


listFolderFiles('test');

?>

这篇关于每个文件夹中的目录和秀文件的文件夹的PHP数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆