PHP Order folders & files in directory in custom order
<p>Sorry for the lengthy post that follows...</p>

<p>I have searched for a few days now on how to do this, and I cannot seem to wrap my head around how to get this done.
I have a directory with twelve folders in it, one for each month of the year (January, February, etc...)</p>

<p>Inside each folder, I have a certain number of image files, desktop backgrounds. Each file is named according to the month it belongs to, the year the month it is for, whether it has a calendar or not, and it's resolution size.
For example the Jan folder would have files with the naming convention:
January2013_nocal_2560x1440; January2012_nocal_2560x1440; etc...</p>

<p>For the current month, such as May, there would also be files that are named:
May2013_calendar_2560x1440; May2013_calendar_1920x1200; etc...</p>

<p>What I would like to do is create a list of the folders, in the order that each month follows in a year, starting with the current month and working backwards in time. Under each month, I would like to list the files in that folder that are for the correct year.</p>

<p>Here is what I would like output.
For the Current Month:</p>

<pre><code>&lt;a class="menuitem" href=""&gt;May 2013&lt;/a&gt;
&lt;ul class="list2" style="float:left"&gt;Without Calendar:
    &lt;li&gt;&lt;a href="May/May2013_nocal_2560x1440.jpg"&gt;2560 x 1440&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="May/May2013_nocal_1920x1200.jpg"&gt;1920 x 1200&lt;/a&gt;&lt;/li&gt;
    etc...
&lt;/ul&gt;
&lt;ul class="list2" style="float:right"&gt;With Calendar:
    &lt;li&gt;&lt;a href="May/May2013_calendar_2560x1440.jpg"&gt;2560 x 1440&lt;/a&gt;&lt;/li&gt;
    &lt;li&gt;&lt;a href="May/May2013_calendar_1920x1200.jpg"&gt;1920 x 1200&lt;/a&gt;&lt;/li&gt;
    etc...
&lt;/ul&gt;
</code></pre>

<p>For each following month:</p>

<pre><code>&lt;a class="menuitem" href=""&gt;April 2013&lt;/a&gt;
&lt;ul class="list2" style="float:left"&gt;Without Calendar:
    &lt;li&gt;&lt;a href="April/April2013_nocal_2560x1440.jpg"&gt;2560 x 1440&lt;/a&gt;&lt;/li&gt;
    etc...
&lt;/ul&gt;
&lt;a class="menuitem" href=""&gt;March 2013&lt;/a&gt;
&lt;ul class="list2" style="float:left"&gt;Without Calendar:
    &lt;li&gt;&lt;a href="March/March2013_nocal_2560x1440.jpg"&gt;2560 x 1440&lt;/a&gt;&lt;/li&gt;
    etc...
&lt;/ul&gt;
&lt;a class="menuitem" href=""&gt;February 2013&lt;/a&gt;
&lt;ul class="list2" style="float:left"&gt;Without Calendar:
    &lt;li&gt;&lt;a href="February/February2013_nocal_2560x1440.jpg"&gt;2560 x 1440&lt;/a&gt;&lt;/li&gt;
    etc...
&lt;/ul&gt;
&lt;a class="menuitem" href=""&gt;January 2013&lt;/a&gt;
&lt;ul class="list2" style="float:left"&gt;Without Calendar:
    &lt;li&gt;&lt;a href="January/January2013_nocal_2560x1440.jpg"&gt;2560 x 1440&lt;/a&gt;&lt;/li&gt;
    etc...
&lt;/ul&gt;
&lt;a class="menuitem" href=""&gt;December 2012&lt;/a&gt;
&lt;ul class="list2" style="float:left"&gt;Without Calendar:
    &lt;li&gt;&lt;a href="December/December2012_nocal_2560x1440.jpg"&gt;2560 x 1440&lt;/a&gt;&lt;/li&gt;
    etc...
&lt;/ul&gt;
&lt;a class="menuitem" href=""&gt;November 2012&lt;/a&gt;
&lt;ul class="list2" style="float:left"&gt;Without Calendar:
    &lt;li&gt;&lt;a href="November/November2012_nocal_2560x1440.jpg"&gt;2560 x 1440&lt;/a&gt;&lt;/li&gt;
    etc...
&lt;/ul&gt;

etc...
</code></pre>

<p>I want it to loop through each folder, in calendar order, and echo out each file in the folder for 2013, then loop through all folders again for 2012, and so on.</p>

<p>I have researched <code>scandir</code>, <code>blob</code>, <code>explode</code> and other resources to read/list each folder and file, but cannot figure out how to order everything how I want it to.</p>

<p>Any advice on how to do this would greatly be appreciated.</p>

<p>EDIT:
I have been searching to no avail the past few days. I appreciate all the responses I have received so far, but none of them seem to actually do what I am wanting to do.
Here is my base code so far, which pulls all of the folders/files I am wanting to sort and loop through.</p>

<pre><code>$root = $_SERVER['DOCUMENT_ROOT']."/wallpapers/";

$folders = scandir($root);

foreach ($folders as $folder) {
if(is_dir($root . "/" . $folder) &amp;&amp; $folder != "." &amp;&amp; $folder != ".." &amp;&amp; $folder !=    "images") {
    $months = array($folder);
    foreach ($months as &amp;$month) {
        $files = scandir($root . $month);
        foreach ($files as $file) {
            if(is_file($root . $month . "/" . $file)) {
                echo $month . "/" . $file . "&lt;br /&gt;";
            }
        }
    }
} 
} 
</code></pre>

<p>I am just not sure where to go from here.</p>
