Search through server folders and display files that exist
You might want to try using the scandir function to build your arrays of directories and files, makes it a bit more future proof.

$baseDir = ABSPATH."/uploads";
$calendarinfo = cal_info(0); //built-in function to generate calendar info, e.g. month names
$years = scandir($baseDir);

//You might want to ignore certain folders, e.g. on UNIX systems:
$ignore = [".","..",".DS_Store"];

foreach ($years as $year) {

    if (!in_array($year, $ignore)) { //the folder is not in the ignore array

        //then scan the year directory for user folders
        $users = scandir($baseDir."/".$year);
        foreach ($users as $user) {

            if ($user == $username) { //if the year contains a folder belonging to logged in user
                echo "&lt;h1&gt;".$year."&lt;/h1&gt;"; //create a year heading
                $months = scandir($baseDir."/".$year."/".$user); //scan it for month folders
                foreach ($months as $month) { //scan for month folders

                if (!in_array($month, $ignore)) { //ignore specified folders

                    echo "&lt;div class='month-box' id='box$month'&gt;";
                    echo "&lt;h2&gt;".$calendarinfo["months"][$month]."&lt;/h2&gt;";

                    $contents = scandir($baseDir."/".$year."/".$user."/".$month); //scan each month folder
                    foreach ($contents as $key=&gt;$item) { //for every item found in the month folder

                        if (!is_dir($baseDir."/".$year."/".$user."/".$month."/".$item)) { //ignore directories
                            //and list out the files
                            echo "&lt;a class='download-link' id='download-link-$key' href='".$host."/uploads/".$year.'/'.$username.'/'.$month.'/'.$item."'&gt;Download&lt;/a&gt;";
                        }
                    }

                    echo "&lt;/div&gt;";

                }
            }
        }
    }
}
}