TheGiantBaboon
TheGiantBaboon

Reputation: 89

How do I display folders in date of creation order?

I am new to PHP and developing a project called BaboonHut.com, I am coding it in PHP as the best way to learn is by just diving in. Anyway to the question, the snippet of code below currently gets the name of all the folders in a certain location and displays some information, however the items are currently displaying in alphabetical order and I would like them to display in order of folder creation. SimpleHost should be in front of Code-Game-Sleep http://www.baboonhut.com/resources/, Thanks in advance.

<?php
$dir = 'resources/';

foreach(glob($dir.'*', GLOB_ONLYDIR) as $resdir) {
    $resdir = str_replace($dir, '', $resdir);
    echo "
<div class=\"span3\">
<div class=\"tile\">
<img src=\"resources/". $resdir ."/thumbnail.png\" class=\"img-rounded\">
<h3 class=\"tile-title\">". $resdir ."</h3>
<p>"; echo  include('resources/'. $resdir .'/description.txt'); echo "</p>
<a class=\"btn btn-primary btn-large btn-block\" href=\"http://www.baboonhut.com/resources/" . $resdir ."/\">More Information</a>
</div>
</div>
"
;
}
?>

Upvotes: 2

Views: 687

Answers (3)

Get Off My Lawn
Get Off My Lawn

Reputation: 36351

This will grab a list of files from a directory put them into an array then sort the array by date.

<?php
$dir = 'resources/';
$files = [];

foreach(glob($dir.'*', GLOB_ONLYDIR) as $resdir) {
    $files[] = [
        "name" => $resdir,
        "time" => filectime($resdir)
    ];
}

// Sort files by date
usort($files, function($a, $b){
    return $b["time"] - $a["time"];
});

foreach($files as $resdir) {
    $resdir = str_replace($dir, '', $resdir);
    echo <<<HTML
    <div class="span3">
    <div class="tile">
    <img src="resources/$resdir/thumbnail.png" class="img-rounded">
    <h3 class="tile-title">$resdir</h3>
    <p>
HTML;
    readfile('resources/'. $resdir .'/description.txt'); 
    echo <<<HTML
    </p>
    <a class="btn btn-primary btn-large btn-block" href="http://www.baboonhut.com/resources/$resdir/">More Information</a>
    </div>
    </div>
HTML;
}

Upvotes: 2

You can't beat DirectoryIterator.

$files = array();
$dir = new DirectoryIterator('.');
foreach ($dir as $fileinfo) {
   // Add only directories into a associative array, that key is it `MTime`
   if($fileinfo->isDir()){
       $files[$fileinfo->getMTime()] = $fileinfo->getFilename();
   }
}

// Then, key sort it.
ksort($files);

Upvotes: 1

Baba
Baba

Reputation: 95161

Using Creation Date

$files = glob(__DIR__ . '/*', GLOB_ONLYDIR);

// Using Creation Date
usort($files, function ($a, $b) {
    return filetime($a) - filetime($b);
});

foreach($files as $file) {
    printf("%s : %s\n", date("r", fileatime($file)), $file);
}

function filetime($file) {
    return PHP_OS == "win" ? fileatime($file) : filectime($file);
}

Using Modification Date

$files = glob(__DIR__ . '/*', GLOB_ONLYDIR);

// Using Modification Date
usort($files, function ($a, $b) {
    return filemtime($a) - filemtime($b);
});

foreach($files as $file) {
    printf("%s : %s\n", date("r", fileatime($file)), $file);
}

Note : Using $a - $b in descending other but if you want descending please use $b - $a

Upvotes: 0

Related Questions