1

Okay, I've been searching for a way to list directories and files, which I've figured out and am utilizing code I found here on StackOverflow (Listing all the folders subfolders and files in a directory using php).

So far I've altered code found in one of the answers. I've been able to remove file extensions from both the path and the file name using preg_replace, capitalize the file names using ucwords, and switch out dashes for spaces using str_replace.

What I'm having trouble with now is wrapping the whole thing in a properly nested HTML list. I've managed to set it up so it's wrapped in a list, but it doesn't use nested lists where needed and I can't, for the life of me, figure out how to capitalize the directory names or replace any dashes within the directory name.

So, the questions are, if anyone would be so kind:

  1. How do I wrap the output in properly nested lists?
  2. How do I capitalize directory names while removing the preceding slash and replace dashes or underscores with spaces?

I've left the |&nbsp;&nbsp; within the $ss variable intentionally. I use it as a marker of sorts when I want to throw in characters that will identify where it shows up during trial and error (example $ss = $ss . "<li>workingOrNot").

I'm using:

<?php
$pathLen = 0;

function prePad($level) {
    $ss = "";

    for ($ii = 0;   $ii < $level;   $ii++) {
        $ss = $ss . "|&nbsp;&nbsp;";
    }

    return $ss;
}

function dirScanner($dir, $level, $rootLen) {
    global $pathLen;

    $filesHidden = array(".", "..", '.htaccess', 'resources', 'browserconfig.xml', 'scripts', 'articles');

    if ($handle = opendir($dir)) {

        $fileList = array();

        while (false !== ($entry = readdir($handle))) {
            if ($entry != "." && $entry != ".." && !in_array($entry, $filesHidden)) {
                if (is_dir($dir . "/" . $entry)) {
                    $fileList[] = "F: " . $dir . "/" . $entry;
                }
                else {
                    $fileList[] = "D: " . $dir . "/" . $entry;
                }
            }
        }
        closedir($handle);

        natsort($fileList);

        foreach($fileList as $value) {
            $displayName = ucwords ( str_replace("-", " ", substr(preg_replace('/\\.[^.\\s]{3,5}$/', '', $value), $rootLen + 4)));

            $filePath = substr($value, 3);

            $linkPath = str_replace(" ", "%20", substr(preg_replace('/\\.[^.\\s]{3,5}$/', '', $value), $pathLen + 3));

            if (is_dir($filePath)) {
                echo prePad($level) . "<li>" . $linkPath . "</li>\n";

                dirScanner($filePath, $level + 1, strlen($filePath));

            } else {
                echo "<li>" . prePad($level) . "<a href=\"" . $linkPath . "\" class=\"className\">" . $displayName . "</a></li>\n";
            }
        }
    }
} 

I feel like these answers should be simple, so maybe I've been staring at it too much the last two days or maybe it has become Frankenstein code.

I'm about out of trial and error and I need help.

Jaime
  • 102
  • 8

1 Answers1

1
foreach($fileList as $value) {
    $displayName = ucwords ( str_replace("-", " ", substr(preg_replace('/\\.[^.\\s]{3,5}$/', '', $value), $rootLen + 4)));

    $filePath = substr($value, 3);

    $linkPath = str_replace(" ", "%20", substr(preg_replace('/\\.[^.\\s]{3,5}$/', '', $value), $pathLen + 3));

    if (is_dir($filePath)) {
        // Do not close <li> yet, instead, open an <ul>
        echo prePad($level) . "<li>" . $linkPath; . "<ul>\n";
        dirScanner($filePath, $level + 1, strlen($filePath));
        // Close <li> and <ul>
        echo "</li></ul>\n";
    } else {
        echo "<li>" . prePad($level) . "<a href=\"" . $linkPath . "\" class=\"className\">" . $displayName . "</a></li>\n";
    }
}

I guess you're opening the main before call the function and closing it at the end.

Triby
  • 1,739
  • 1
  • 16
  • 18