Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To hit your question directly, while you're reading the dir for files, you can get info about the files using some native php functions...</p> <p>When a file was last accessed: fileatime - <a href="http://www.php.net/manual/en/function.fileatime.php" rel="nofollow">http://www.php.net/manual/en/function.fileatime.php</a></p> <p>When a file was created: filectime - <a href="http://www.php.net/manual/en/function.filectime.php" rel="nofollow">http://www.php.net/manual/en/function.filectime.php</a></p> <p>When a file was modified: filemtime - <a href="http://php.net/manual/en/function.filemtime.php" rel="nofollow">http://php.net/manual/en/function.filemtime.php</a></p> <p>These return the time, formatted as unix time.</p> <p>For simplicity, I would use filectime to find the time, and use that value as the KEY in the $files array, like so: $files[filectime($file)] = $file;</p> <p>Then you can use a simple array sorting function like ksort() to order them outside the loop, before you start step two.</p> <p>Now... Going slightly deeper... I would probably use a database to store information like this, instead of hitting the file system every time the page is loaded. It will be a little more overhead in development, but depending on the size of the dir, could save you a lot of time and processing power.</p> <p>TESTED 2012-06-23 <code> <pre><code> /* settings */ $image_dir = 'photo_gallery/'; $per_column = 6; /* step one: read directory, make array of files */ if ($handle = opendir($image_dir)) { while (false !== ($file = readdir($handle))) { if ($file != '.' &amp;&amp; $file != '..') { if(strstr($file,'-thumb')) { $files[filemtime($image_dir . $file)] = $file; } } } closedir($handle); } /* step two: loop through, format gallery */ if(count($files)) { krsort($files); foreach($files as $file) { $count++; echo '&lt;a class="photo-link" rel="one-big-group" href="',$image_dir,str_replace('-thumb','',$file),'"&gt;&lt;img src="',$image_dir,$file,'" width="100" height="100" /&gt;&lt;/a&gt;'; if($count % $per_column == 0) { echo '&lt;div class="clear"&gt;&lt;/div&gt;'; } } } else { echo '&lt;p&gt;There are no images in this gallery.&lt;/p&gt;'; } ?&gt; </code></pre> <p></code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload