可读的PHP站点地图:忽略某些目录工作不正确

I'm trying to integrate a PHP script on a website which creates a sitemap and displays it as readable html on the relevant page. I'm not looking for a XML sitemap for search engines but for a readable hierarchical overview of all pages and of the complete website.

Therefore, I used this ready-to-use script http://www.apptools.com/phptools/dynamicsitemap.php and modified it so that it can be used under PHP 7. The changes I made involved mainly the substitution of deprecated functions as recommend by the PHP manual. Since I won't use the integrated JavaScript function, I did not integrate this part of the script too.

Now, the script works fine as I intended it to do. But one problem drives me a bit crazy for some days now. The script displays directories and (sub-)pages in an alphabetical order. When I exclude a subdirectory, e.g. ABC, from the main directory which is the last one in the alphabetically ordered sitemap, the subdirectory disappears in the sitemap - as intended -, but also does the main directory.

Example: Main directory = XYZ, Sub directory = ABC. ABC shall be excluded from the sitemap, so that it is not displayed. When I add "ABC" to the $ignore-array (see code below), it disappears from the sitemap, but also does the main directory "XYZ". This only happens for the last directory in the sitemap-list.

I cannot elucidate where this problem comes from. For all other directories, the code works fine and as far as I can assess it, the code should also work for the last directory in the list.

Can anyone around here help with this issue? Thanks a lot in advance!

<?php
$startin="";

$imgpath="";

$types=array(
".php",
);

$htmltypes=array(
".php",
);

$ignore=array(
"ABC",
);

$id=0;
echo "<div id=\"sitemap\"><ul id=\"list$id\">
";
$id++;
$divs="";
if(substr($startin,strlen($startin)-1,1)=="/")
$startin=trim($startin,"/");
foreach($types as $type){
if (file_exists($_SERVER['DOCUMENT_ROOT']."$startin/index$type")){
    $index=$_SERVER['DOCUMENT_ROOT']."$startin"."/index$type";
    break;
}
}
$types=join($types,"|");
$types="($types)";
if(!is_array($htmltypes))
$htmltypes=array();
if(count($htmltypes)==0)
$htmltypes=$types;
if(!$imgpath)
$imgpath=".";
echo "<li><strong><a href=\"$startin/\">".getTitle($index)."</a></strong>
";
showlist($_SERVER['DOCUMENT_ROOT']."$startin");
echo "</li></ul></div>
";
if (is_array($divs)){
$divs="'".join($divs,"','")."'";
echo "<script type=\"text/javascript\">
";
echo "//<![CDATA[
";
echo "d=Array($divs);
";
echo "for (i=0;i<d.length;i++){
";
echo "\ttoggle('list'+d[i],'img'+d[i]);
";
echo "}
";
echo "//]]>
";
echo "</script>
";
}

function showlist($path){
global $ignore, $id, $divs, $imgpath, $types, $startin;
$dirs=array();
$divs=array();
$files=array();
if(is_dir($path)){
    if ($dir = @opendir($path)) {
        while (($file = readdir($dir)) !== false) {
            if ($file!="." && $file!=".." && !in_array($file,$ignore)){
                if (is_dir("$path/$file")){
                    if (file_exists("$path/$file/index.php"))
                        $dirs[$file]=getTitle("$path/$file/index.php");
                    elseif (file_exists("$path/$file/index.html"))
                        $dirs[$file]=getTitle("$path/$file/index.html");
                    elseif (file_exists("$path/$file/index.htm"))
                        $dirs[$file]=getTitle("$path/$file/index.htm");
                    else
                        $dirs[$file]=$file;
                } else {
                    if (preg_match("$types", $file)){
                        $files[$file]=getTitle("$path/$file");
                        if (strlen($files[$file])==0)
                            $files[$file]=$file;
                    }
                }
            }
      }  
      closedir($dir);
    }
    natcasesort($dirs);
    $url=str_replace($_SERVER['DOCUMENT_ROOT'],"",$path);
    $n=substr_count("$url/$","/");
    $base=substr_count($startin,"/")+1;
    $indent=str_pad("",$n-1,"\t");
    echo "$indent<ul id=\"list$id\">
";
    if ($n>$base)
        $divs[]="$id";
    foreach($dirs as $d=>$t){
        $id++;
        echo "$indent\t<li><a href=\"javascript:toggle('list$id','img$id')\"></a>";
        echo " <strong><a href=\"$url/$d/\">$t</a></strong>
";
        showlist("$path/$d");
        echo "$indent\t</li>
";
    }
    natcasesort($files);
    $id++;
    foreach($files as $f=>$t){
        echo "$indent\t<li> <a href=\"$url/$f\">$t</a></li>
";
    }
    echo "$indent</ul>
";
}
}

function getTitle($file){
global $htmltypes;
$title="";
$p=pathinfo($file);
if(!in_array(strtolower($p['extension']),$htmltypes)){
    $f=file_get_contents($file);
    if(preg_match("'<title>(.+)</title>'i", $f, $matches)){
        $title=$matches[1];
    }
}
$title=$title?$title:basename($file);
return htmlentities(trim(strip_tags($title)));
}
?>