0

I have a webapp which reads and interprets .rptdesign files. All .rptdesgn files are located in a maindirectory within the webapp (./report/)

For instance:

report/
file1.rptdesign
file2.rptdesign
subdirectory1/
    file3.rptdesign
    file4.rptdesign
...

The link to access file3.rptdesign looks like this: server1.com/webapp/frameset?__report=report/subdirectory/file3.rptdesign

I want to have list of all and only .rptdesign files. When I click one the link needs to be build with the directory and the filename. I have something like this which works, if I put all .rptdesign files in a one level directory without any subdirecotries:

<%@ page language="java" import="java.io.*" pageEncoding="UTF-8"%>
<html>
<head>
  <meta content="text/html; charset=UTF-8" />
  <title>Web course code example</title>
</head>
<body>
  <ul style="font-size:16px;">
    <%
    String path = application.getRealPath("report");
    out.println("<p>Reports</p><br/>");
    File f = new File(path);
    File[] files = f.listFiles();
    if(files.length == 0){
        out.println("<p>Nothing</p><br/>");
        return;
    }
    for(int i=0; i<files.length; i++){
        if (files[i].isFile()){
            String fname = files[i].getName();
            int last_index = fname.lastIndexOf(".");
            if(last_index == -1){
                continue;
            }
            String suffix = fname.substring(last_index);
            if(suffix.equals(".rptdesign")){
                out.print("<li>");
                out.print("<a href='./frameset?__report=report/" + fname + "'>");
                out.println(fname);
                out.print("</a>");
                out.print("</li>");
            }
        }
    }
    %>
         </ul>
              </body>
                   </html>

But I want to have something like this: Listing all the folders subfolders and files in a directory using php

<html>
<body>
<?php        
function listFolderFiles($dir){
    $ffs = scandir($dir);

    unset($ffs[array_search('.', $ffs, true)]);
    unset($ffs[array_search('..', $ffs, true)]);

    // prevent empty ordered elements
    if (count($ffs) < 1)
        return;

    echo '<ol>';
    foreach($ffs as $ff){
        echo '<li>'.$ff;
        if(is_dir($dir.'/'.$ff)) listFolderFiles($dir.'/'.$ff);
        echo '</li>';
    }
    echo '</ol>';
}

listFolderFiles('Main Dir');
?>
</body>
</html>

The problem is that in this mentioned php code I am little bit lost, how I can add the href part and how I can only filter all the .rptdesign files like I did in my jsp with the suffix if. At the end I want to see the list like this in the browser and be able to click the correct builded links to the .rptdesign files:

file1.rptdesign
file2.rptdesign
subdirectory1/
    file3.rptdesign
    file4.rptdesign
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
siemkev
  • 1
  • 2
  • Java and JSP are not JavaScript; please amend the tags in your question ;) – secan Jul 21 '21 at 10:44
  • changed sorry :) – siemkev Jul 21 '21 at 10:47
  • 1
    I’m confused, what language are you writing this in? – Chris Haas Jul 21 '21 at 11:14
  • first one is .jsp the second .php its pretty much about that I want to have the rptdesign filter (if(suffix..)) and href generation part of the .jsp in the .php approach. But I am also open to other solutions, it is just my approach. – siemkev Jul 21 '21 at 11:21
  • _how I can add the href part_ Well just like the `.jsp` you have to add an anchor tag and fill the `href` attr with relevant info. You are still generating HTML, nothing else – RiggsFolly Jul 21 '21 at 11:28

1 Answers1

0
<html>
<body>
<?php
define('SITE_URL',"http://server1.com:8080/Birt/frameset?__report=");

function listFolderFiles($dir){
   $fileFolderList = scandir($dir);
   echo '<ul style="list-style: none;">';
   foreach($fileFolderList as $fileFolder){
       if($fileFolder != '.' && $fileFolder != '..'){
           if(!is_dir($dir.'/'.$fileFolder)){
               echo '<li><a target="_blank" href="'.SITE_URL.ltrim($dir.'/'.$fileFolder,'./').'">'.$fileFolder.'</a>';
           } else {
               echo '<li>'.$fileFolder;
           }
           if(is_dir($dir.'/'.$fileFolder)) listFolderFiles($dir.'/'.$fileFolder);
               echo '</li>';
           }
   }
   echo '</ul>';
}
listFolderFiles('report/');
?>
</body>
</html>

This works! But its format is really ugly:

https://i.stack.imgur.com/ee41A.png

Someone knows how to sort/format it better? So that it's always the directory with all files first and than the list proceed with the next directory with all files? In the picture which I posted I would prefer it like this:

Dock_to_Stock.rptdesign
salesinvoice.rptdesign
test.rptdesign
dir1
test2.rptdesign
test3.rptdesign
    dir2
        test4.rptdesign
        test5.rptdesign
        test6.rptdesign
siemkev
  • 1
  • 2