PHP Order folders & files in directory in custom order -
sorry lengthy post follows...
i have searched few days on how this, , cannot seem wrap head around how done. have directory twelve folders in it, 1 each month of year (january, february, etc...)
inside each folder, have number of image files, desktop backgrounds. each file named according month belongs to, year month for, whether has calendar or not, , it's resolution size. example jan folder have files naming convention: january2013_nocal_2560x1440; january2012_nocal_2560x1440; etc...
for current month, such may, there files named: may2013_calendar_2560x1440; may2013_calendar_1920x1200; etc...
what create list of folders, in order each month follows in year, starting current month , working backwards in time. under each month, list files in folder correct year.
here output. 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 loop through each folder, in calendar order, , echo out each file in folder 2013, loop through folders again 2012, , on.
i have researched scandir
, blob
, explode
, other resources read/list each folder , file, cannot figure out how order how want to.
any advice on how appreciated.
edit: have been searching no avail past few days. appreciate responses have received far, none of them seem wanting do. here base code far, pulls of folders/files wanting sort , loop through.
$root = $_server['document_root']."/wallpapers/"; $folders = scandir($root); foreach ($folders $folder) { if(is_dir($root . "/" . $folder) && $folder != "." && $folder != ".." && $folder != "images") { $months = array($folder); foreach ($months &$month) { $files = scandir($root . $month); foreach ($files $file) { if(is_file($root . $month . "/" . $file)) { echo $month . "/" . $file . "<br />"; } } } } }
i not sure go here.
you can list files in directory want , can use usort
Comments
Post a Comment