Sorry for the lengthy post that follows...
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...)
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...
For the current month, such as May, there would also be files that are named: May2013_calendar_2560x1440; May2013_calendar_1920x1200; etc...
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.
Here is what I would like output. For the Current Month:
<a class="menuitem" href="">May 2013</a>
<ul class="list2" style="float:left">Without Calendar:
<li><a href="May/May2013_nocal_2560x1440.jpg">2560 x 1440</a></li>
<li><a href="May/May2013_nocal_1920x1200.jpg">1920 x 1200</a></li>
etc...
</ul>
<ul class="list2" style="float:right">With Calendar:
<li><a href="May/May2013_calendar_2560x1440.jpg">2560 x 1440</a></li>
<li><a href="May/May2013_calendar_1920x1200.jpg">1920 x 1200</a></li>
etc...
</ul>
For each following month:
<a class="menuitem" href="">April 2013</a>
<ul class="list2" style="float:left">Without Calendar:
<li><a href="April/April2013_nocal_2560x1440.jpg">2560 x 1440</a></li>
etc...
</ul>
<a class="menuitem" href="">March 2013</a>
<ul class="list2" style="float:left">Without Calendar:
<li><a href="March/March2013_nocal_2560x1440.jpg">2560 x 1440</a></li>
etc...
</ul>
<a class="menuitem" href="">February 2013</a>
<ul class="list2" style="float:left">Without Calendar:
<li><a href="February/February2013_nocal_2560x1440.jpg">2560 x 1440</a></li>
etc...
</ul>
<a class="menuitem" href="">January 2013</a>
<ul class="list2" style="float:left">Without Calendar:
<li><a href="January/January2013_nocal_2560x1440.jpg">2560 x 1440</a></li>
etc...
</ul>
<a class="menuitem" href="">December 2012</a>
<ul class="list2" style="float:left">Without Calendar:
<li><a href="December/December2012_nocal_2560x1440.jpg">2560 x 1440</a></li>
etc...
</ul>
<a class="menuitem" href="">November 2012</a>
<ul class="list2" style="float:left">Without Calendar:
<li><a href="November/November2012_nocal_2560x1440.jpg">2560 x 1440</a></li>
etc...
</ul>
etc...
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.
I have researched scandir
, blob
, explode
and other resources to read/list each folder and file, but cannot figure out how to order everything how I want it to.
Any advice on how to do this would greatly be appreciated.
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.
$root = $_SERVER['DOCUMENT_ROOT']."/wallpapers/";
$folders = scandir($root);
foreach ($folders as $folder) {
if(is_dir($root . "/" . $folder) && $folder != "." && $folder != ".." && $folder != "images") {
$months = array($folder);
foreach ($months as &$month) {
$files = scandir($root . $month);
foreach ($files as $file) {
if(is_file($root . $month . "/" . $file)) {
echo $month . "/" . $file . "<br />";
}
}
}
}
}
I am just not sure where to go from here.