Using previous questions about this topic I have been able to create a recursive function to display the contents of a directory in a menu however, I have run into an issue with the recursion depth when it comes to the actual file names.
When the depth of the directory increases I cannot figure out how to store/reference the parent directory path such that the href is correct.
With the below code and an example directory structure like below the file path which I'm creating for the href works for top level directories but I need a way to preserve the parent directory paths as it goes deeper in the directory structure. The link to file.html inside InnerSection for example simple generates as file/InnerSection/file.html instead of files/Section2/Innersection/file.html.
Any help would be appreciated.
Files Section1 Section2 file.html InnerSection file.html
function findDir($path){
$dir=directory_map($path, 0);
return $this->recursive($dir, $path);
}
function recursive($arr, $path) {
global $menu;
global $current_path;
foreach ($arr as $key => $val) {
if (is_array($val)){
$current_path =rtrim($key, '\\');
$menu.= "<li data-role='collapsible' data-iconpos='right' data-shadow='false' data-corners='false'>";
$menu.= "<h3>".rtrim($key, "\\")."</h3>";
$menu.= "<ul data-role='listview' data-shadow='false' data-inset='true' data-corners='false'>";
$this->recursive($val, $path);
$menu.= "</ul></li>"; //finish directory listing
} else {
$menu.= "<li><a class='load-file' data-file='". $path."/".$current_path."/".$val . "' href='". $path . "/" . $current_path . "/" . "$val''>".$val."</a></li>"; //List file
}
}
return $menu;
}