I have following PHP function which return all the folder with files in html li
element from a given path.
function folderTree ($directory_path) {
if(!file_exists($directory_path)) {
die("The file $directory_path is not exists");
}
if(!is_dir($directory_path)) {
die("This directory $directory_path can't open");
}
$directory = opendir($directory_path);
$filenames = [];
while ($filename = readdir($directory)) {
if($filename !== '.' && $filename !== '..') {
if(is_dir($directory_path.'/'.$filename)) {
$filename .= '/';
}
$filenames[] = $filename;
}
}
echo "<ul>";
foreach ($filenames as $filename) {
echo "<li><a href='{$directory_path}/{$filename}'>";
echo $filename;
if(substr($filename, -1) == '/' ) {
folderTree($directory_path.'/'.substr($filename, 0, -1)).'/lv2/';
}
echo "</a></li>";
}
echo "</ul>";
}
now It's working fine and the output this below:
img-01
img01.jpg
img02.jpg
img03.jpg
img-02
img01.jpg
img02.jpg
img03.jpg
img-03
img01.jpg
img02.jpg
img03.jpg
This img-01
, 02
and 03
is a folder and there are .jpg
files exist. Now, You can see the code above that it's wrapping all the files of all folders in a single ul
element, right.
But I want it should wrap each folder by folder not all at once.
Now the code output is something like that:
<ul><li>jpg...</li><li>jpg...</li><li>jpg...</li><li>jpg...</li> só on..</ul>
But I need:
<ul><li>jpg...</li><li>jpg...</li><li>jpg...</li></ul>
<ul><li>jpg...</li><li>jpg...</li><li>jpg...</li></ul>
<ul><li>jpg...</li><li>jpg...</li><li>jpg...</li></ul>