This is a follow up to a previous post http://sheldon.lendrum.co.nz/clean-dynamic-directory-listing-with-php_206/04/ that will Dynamically display the files in a set directory.
In this version the files can be ordered by the date that they were last modified. I will also explain how to order both Ascending and Descending by the date the file was created also.
Example Usages:
< ?php // EAMPLE USAGE // SHOWS THE 10 NEWEST .php FILES IN A UNORDERED LIST echo("<ul>". direcotryList("10","./",".php","<li>","</li>\n") .""); // SHOWS ALL JPG IMAGES AS THUMBNAILS echo(direcotryList("","./images/",".jpg","<img src=\"","\" width=\"150\" />\n")); ?>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | < ?php // DIRECTORY DISPLAY USING GLOB SORTNG BY LAST MIDIFIED FILE DATE. // IF ALL PERIMATORS ARE EMPTY WILL SCAN DEFAULT DIRECTORY // '$display' => AMOUNT OF RECORDS TO DISPLAY // '$path' => PATH TO DIRECOTRY TO SCAN // '$ext' => FILES EXT, IF YOU WANT OT ECLUDE IT, OR ONLY SCAN FOR ONE TYPE OF FILE // '$start' => APPEND TO START OF OUTPUT LINE EG: '<li>' OR '<div class="list">' // '$end' => APPEND TO END OF OUTPUT LINE EG: '</div></li>' OR '\n' => DEFAULT '\n' function direcotryList($path = "./",$display = NULL, $ext = NULL, $start = NULL, $end = "\n"){ $return = (int)0; $output = ""; $ignore = array(".","..","thumbs.db"); if(empty($display)){ $display = time() * 1000; } if(($count = count($files = glob($path ."*". $ext))) > 0){ foreach($files as $v){ $filemtime[] = filemtime($v); } array_multisort($filemtime, SORT_DESC, $files); foreach($files as $file){ if($return < = $display){ if(is_file($file) and !in_array($file, $ignore)){ $output .= $start . $file . $end; $return++; } } } } return $output; } ?> |
Alterations:
To order the directory by Creation time rather than the current Modified date Alter line 17 to filectime.
17 | $filemtime[] = filectime($v); |
Currently the output is orders Newest to Oldest, to reverse the Order, change the Constant in the array_multisort on line 19.
19 | array_multisort($filemtime, SORT_DESC, $files); |
If you have any comments or suggestions, please ask below.
Great site. Good info