Search through server folders and display files that exist I have a folder called uploads in this folder is a folder for the year the file was uploaded(2013,2014,2015 etc...), inside the year folder are usernames(rick, dan, edward etc...), inside the usernames folder are folders 1-12 for the month that the file was uploaded.

I want to search through the folders and if there is a file in the folder display the link, the problem I am having is figuring out how to do the check on all of the folders:

Here is my code so far:

&lt;?php

        //array for years to check for
        $year = array('2013','2014','2015','2016','2017','2018','2019','2020');
        $month = array('1','2','3','4','5','6','7','8','9','10','11','12');

        //loop through years array and populate $existingYears[] array with years that exist on server
        foreach($year as $years){

            if(is_dir($yearPath = ABSPATH."/"."uploads/".$years."/".$username)){

                $existingYears[] = $years;

            }

        }

        //loop through existings years
        foreach($existingYears as $year){

            //title
            print "&lt;h1&gt;".$year."&lt;/h1&gt;";

                //loop for months
                for ($i=1; $i&lt;=12; $i++) {

                    if($i == 1){
                        $title = "January";
                    } elseif($i == 2){
                        $title = "February";
                    }elseif($i == 3){
                        $title = "March";
                    }elseif($i == 4){
                        $title = "April";
                    }elseif($i == 5){
                        $title = "May";
                    }elseif($i == 6){
                        $title = "June";
                    }elseif($i == 7){
                        $title = "July";
                    }elseif($i == 8){
                        $title = "August";
                    }elseif($i == 9){
                        $title = "September";
                    }elseif($i == 10){
                        $title = "October";
                    }elseif($i == 11){
                        $title = "November";
                    }elseif($i == 12){
                        $title = "December";
                    }

                    //path to months directories
                    $url=$userPath.'/'.$i;

                    $newUrl = $url.'/'.$files[2];

                    print $newUrl;

                    //check if directory exists
                    if(is_dir($url)){

                        //assign open state to $dir
                        $dir = opendir($url);

                        //add all files to $files[] array
                        while (($file = readdir($dir)) !== false){

                            $files[] = $file;

                        }
                        closedir($dir);

                        //display link to payslips
                        print "&lt;div class='month-box' id='box$i'&gt;";
                        print "&lt;h2&gt;".$title."&lt;/h2&gt;";
                        print "&lt;a class='download-link' id='download-link-$i' href='".$host."/uploads/".$year.'/'.$username.'/'.$i.'/'.$files[2]."'&gt;Download&lt;/a&gt;";
                        print "&lt;/div&gt;";

                    }

                }

        }

        ?&gt;


Very messy I know, I am sure there is a better way to do this just not sure how.
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;";

                }
            }
        }
    }
}
}