0

I have a folder structure like this

Folder/
  09_06_2014/
    Main Body/
     page001.JPG 

Folder/
  03_06_2014/
    Main Body/
     page001.JPG

Folder/
  09_07_2014/
    Main Body/
     page001.JPG

What i'm trying to do is pick the picture page001.JPG from the most recent folder (09_07_2014) with the intention of having a dropdown menu with the subsequent entries.

my code block looks like this.

$dp = opendir('Folder/.');
while ($file = readdir ($dp)) {
  if ($file != '.' && $file != '..') {
     echo "<pre>";
     echo "<a href='";
     echo "$file/page001.JPG'>$file</a>\n";
     echo "</pre>";
  }
}
closedir($dp);

Someone suggested i load the folder names into a 3 dimensional array but that's out of my depth of knowledge :( Any advice would be great

GreyRoofPigeon
  • 17,833
  • 4
  • 36
  • 59
DiZzY
  • 35
  • 6
  • 1
    You should have named your folders as yyyy_mm_dd, then sorting would be easy. Is it too late to change? – vascowhite Jul 09 '14 at 11:16
  • they were running that way from a script that we had no access to change. so cast in stone would be accurate :) – DiZzY Jul 09 '14 at 12:16

2 Answers2

0

Easiest way to to rename your date folder to YYYYMMDD format. With that, it must be straight forward to sort strings in descending order.

However if you cant rename your folders, get them in an array and write custom sort function and use usort.

Kapil Sharma
  • 10,135
  • 8
  • 37
  • 66
0

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");
    }
}
?>
thedom
  • 2,498
  • 20
  • 26