Skip to content
Advertisement

Get name of subfolder with highest number

A folder ‘archive’ contains lots of numbered sub-folders (starting with 1 and increasing by one weekly, currently 122) like this:

ROOT/archive/1
ROOT/archive/2
ROOT/archive/3
...
ROOT/archive/122

On my homepage (ROOT/index.php) I’d like to show the number of the subfolder with the highest number, so currently ‘122’. I just need to display that number. How do I do that? Thanks for your help!

Advertisement

Answer

A quick and dirty snippet to find the highest numbered directory in archive. You’ll need to set the right path to your archive and add some error handling.

$topDir = 0;
foreach (new DirectoryIterator("archive") as $fileInfo) {
    if ($fileInfo->isDir() && !$fileInfo->isDot()) {
        $topDir = max($topDir, (int)$fileInfo->getBasename());
    }
}

echo "Latest folder: $topDir";
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement