[英]PHP recursively find files and folders and create HTML
Basically what I am trying to do is take a base directory (already defined) and recursively go down it, making a link in a navbar for files and a collapsible for directories; 基本上,我想做的是获取一个基本目录(已定义)并递归地将其向下,在导航栏中创建一个用于文件的链接,并在目录中创建一个可折叠的链接。 under the collapsible would be links for files in that directory and possibly more collapsibles for directories inside the directory.
下方的可折叠文件将是该目录中文件的链接,而目录中的目录可能会更折叠。 Here is what I have so far, which is not working:
这是我到目前为止无法使用的内容:
foreach (new DirectoryIterator($dir) as $file) {
if ($file->isDot()) continue;
if ($file->isFile()) {
echo "<li><a href=\"#!\">" . $file . "</a></li>"; /*This successfully
creates links named from all the files in the directory specified by $dir */
}
if ($file->isDir()) {
?>
<li class="no-padding">
<ul class="collapsible collapsible-accordion">
<li>
<a class="collapsible-header"><?php echo $file->getFilename(); ?></a>
<div class="collapsible-body">
<ul>
<?php
$dir2 = $file->getFilename();
foreach (new DirectoryIterator($dir."/".$dir2) as $file1) {
if ($file1->isDot()) continue;
if ($file1->isFile()) {
echo "<li><a href='#' class=\"mui-btn mui-btn-primary waves-effect waves-teal\">".$file1 . "</a></li>"; }
} ?>
</ul>
</div>
</li>
</ul>
</li>
<?php
}
}
?>
However, for some reason it seems a DirectoryIterator within a DirectoryIterator is no good (the $dir2 was for test purposes). 但是,由于某种原因,DirectoryIterator中的DirectoryIterator似乎不好($ dir2用于测试)。 Or I could have a syntax issue.
或者我可能有语法问题。 The reason I want this to work this way is because I want the files to be contained in
<li><a href="#!">file</a></li>
html and the files under a directory to be in <li><a href='#' class="mui-btn mui-btn-primary waves-effect waves-teal">file-in-folder</a></li>
format. 我希望这样做的原因是因为我希望文件包含在
<li><a href="#!">file</a></li>
html中,而目录下的文件应包含在<li><a href='#' class="mui-btn mui-btn-primary waves-effect waves-teal">file-in-folder</a></li>
格式。
If there is a better way to do this, please let me know! 如果有更好的方法可以进行此操作,请告诉我! Thanks!
谢谢!
EDIT: I changed $1file to $file1 and it works fine, even appended the dir; 编辑:我将$ 1file更改为$ file1,它工作正常,甚至附加了dir; however, I basically want something like a for loop that will keep creating the collapsibles within each other if there is still a directory present.
但是,我基本上想要一个for循环之类的东西,如果仍然存在目录,它们将继续在彼此之间创建可折叠对象。 Basically so it would automatically create the formatting if I had:
基本上,如果我有以下内容,它将自动创建格式:
Dir1 (main) >
File 1 //link
File 2 //link
Dir 1 > //collapsible header
File 1.1 //collapsible button
File 1.2 //...
Dir 2 >
File 2.1
File 2.2
Dir 3
Without me having to write 10 nested DirectoryIterators, just in case you can go down that far. 无需我编写10个嵌套的DirectoryIterators,以防万一您可以走那么远。
I think you are using an invalid variable name. 我认为您使用的是无效的变量名。 PHP accepts variables starting with letters and underscore followed by other chars.
PHP接受以字母和下划线开头,后跟其他字符的变量。 Try $file1 instead of $1file Cheers!
尝试用$ file1代替$ 1file干杯! http://php.net/manual/en/language.variables.basics.php
http://php.net/manual/zh/language.variables.basics.php
This is my logic, probably you can get some idea from here. 这是我的逻辑,也许您可以从这里得到一些想法。
if your main folder looks like this 如果您的主文件夹看起来像这样
then here is the code to get the folder structures. 然后这是获取文件夹结构的代码。
echo '<pre>';
$dir='folders';
$files = array_slice(scandir($dir), 2);
foreach($files as $k=>$file){
$dir1=$dir.'/'.$file;
if(is_dir($dir1)){
$files1 = array_slice(scandir($dir1), 2);
if(!$files1){
$FOLDERS[$file]=$file;
}else{
foreach($files1 as $kk=>$file1){
$dir2=$dir1.'/'.$file1;
$files2 = array_slice(scandir($dir2), 2);
if(!$files2){
$FOLDERS[$file][$file1]=$file1;
}else{
foreach($files2 as $file2){
$FOLDERS[$file][$file1][$file2]=$file2;
}
}
}
}
}
}
print_r($FOLDERS);
Output: 输出:
Array
(
[folder1] => folder1
[folder2] => Array
(
[folder3] => Array
(
[folder4] => folder4
)
[folder5] => folder5
[folder6] => folder6
)
) )
i used it only to scan the folders, you can extend it according to your needs. 我只用它来扫描文件夹,可以根据需要扩展它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.