Listing last 3 days items in directory using PHP -
i have following php code list data directory folder, question how list values last 3 days? secondly, asking possible add pagination code?
<?php function getfilelist($dir) { // array hold return value $retval = array(); // add trailing slash if missing if(substr($dir, -1) != "/") $dir .= "/"; // open pointer directory , read list of files $d = @dir($dir) or die("getfilelist: failed opening directory $dir reading"); while(false !== ($entry = $d->read())) { // skip hidden files if($entry[0] == ".") continue; if(is_dir("$dir$entry")) { $retval[] = array( "name" => "$dir$entry/", "type" => filetype("$dir$entry"), "size" => 0, "lastmod" => filemtime("$dir$entry") ); } elseif(is_readable("$dir$entry")) { $retval[] = array( "name" => "$dir$entry", "type" => mime_content_type("$dir$entry"), "size" => filesize("$dir$entry"), "lastmod" => filemtime("$dir$entry") ); } } $d->close(); return $retval; } ?> <h1>display png images in table</h1> <table class="collapse" border="1"> <thead> <tr><th></th><th>name</th><th>type</th><th>size</th><th>last modified</th></tr> </thead> <tbody> <?php $dirlist = getfilelist("./images"); foreach($dirlist $file) { if(!preg_match("/\.png$/", $file['name'])) continue; echo "<tr>\n"; echo "<td><img src=\"{$file['name']}\" width=\"64\" alt=\"\"></td>\n"; echo "<td>{$file['name']}</td>\n"; echo "<td>{$file['type']}</td>\n"; echo "<td>{$file['size']}</td>\n"; echo "<td>",date('r', $file['lastmod']),"</td>\n"; echo "</tr>\n"; } ?> </tbody> </table>
if want list files modified less 3 days ago
if(!preg_match("/\.png$/", $file['name']) || $file['lastmod'] > strtotime('-3 day') ) continue;
if want add pagination, should put filter in getfilelist
instead of on display can count # of displayable items have.
Comments
Post a Comment