How can I display latest uploaded image first? (PHP+CSS) -
i new php , trying display image gallery retrieves photos folder. thing is, want image recent upload date appear @ beginning, , on until oldest 1 in bottom.
this php looks (the important bit guess)
$files = scandir('uploads/thumbs'); $ignore = array( 'cgi-bin', '.', '..'); foreach ($files $file) { if(!in_array($file, $ignore)) { echo '<img src="uploads/thumbs/' . $file . '" />'; } }
i know if there's way php or maybe little of css display them in reverse order, making newest 1 appear @ top of page.
any or suggestion appreciated, regards argentina!
next $files
can obtain modification time of each file , sort $files
array based on time values acquired. function sorts 2 or more arrays value of array array_multisort
:
$files = scandir($path); $ignore = array( 'cgi-bin', '.', '..'); # removing ignored files $files = array_filter($files, function($file) use ($ignore) {return !in_array($file, $ignore);}); # getting modification time each file $times = array_map(function($file) use ($path) {return filemtime("$path/$file");}, $files); # sort times array while sorting files array array_multisort($times, sort_desc, sort_numeric, $files); foreach ($files $file) { echo '<img src="uploads/thumbs/' . $file . '" />'; }
Comments
Post a Comment