I've implemented some code for you. I've written it as easy as possible (at least I hope I did ;-)). No oop - just procedural code.
The easiest way would have been to rename the folders as mentioned before. It is not performant - but made for learning purposes.
Hope it will help you out!
<?php
//
// Setup.
//
date_default_timezone_set('Europe/Vienna');
$rootDirectory = 'Folder/'; // Needs trailing slash!
$fileToInclude = 'Main Body/page001.JPG';
//
// Execute.
//
if(!is_dir($rootDirectory)) {
die('Directory "'. $rootDirectory .'" does not exist');
}
// Find directories in $rootDirectory.
$directories = array();
if($fh = opendir($rootDirectory)) {
while(false !== ($file = readdir($fh))) {
if($file != '.' && $file != '..') {
$absoluteFilePath = $rootDirectory . $file;
if(is_dir($absoluteFilePath)
&& strlen($file) == 10
&& substr_count($file, '_') == 2) {
$directories[] = $file;
}
}
}
closedir($fh);
}
// Check if directories are found.
if(count($directories) == 0) {
die('No directories found');
}
// Sort directories.
usort($directories, function($a, $b) {
// Transform directory name into UNIX timestamps.
$aTime = mktime(0, 0, 0, substr($a, 3, 2), substr($a, 0, 2), substr($a, -4));
$bTime = mktime(0, 0, 0, substr($b, 3, 2), substr($b, 0, 2), substr($b, -4));
// Compare.
if($bTime > $aTime) {
return 1;
} elseif($bTime < $aTime) {
return -1;
} else {
return 0;
}
});
// Loop through directories in that order.
foreach($directories as $directory) {
// Determine path to directory.
$absoluteDirectoryPath = $rootDirectory . $directory;
if(substr($directory, -1) != DIRECTORY_SEPARATOR) {
$absoluteDirectoryPath .= DIRECTORY_SEPARATOR;
}
// Find $fileToInclude in $absoluteDirectoryPath.
$filePath = $absoluteDirectoryPath . $fileToInclude;
if(file_exists($filePath)) {
echo('<a href="'. $filePath . '">' . $filePath . '</a><br />' . "\n");
}
}
?>