Jump to content

How can I sort images in a directory by the date they were uploaded?


pneudralics

Recommended Posts

I'm using this script, but am failing trying to sort it by the newest uploaded images first.

http://www.ricocheting.com/scripts/gallery.html

 

The below does it alphabetically.

function GetFileList($dirname="."){
global $config;
$list = array(); 

if ($handle = opendir($dirname)) {
	while (false !== ($file = readdir($handle))) {
		//this matches what type of files to get. jpeg, jpg, gif, png (ignoring case)
		if (preg_match("/\.(jpe?g|gif|png)$/i",$file)) { 
			$list[] = $file;
		} 
	}
	closedir($handle); 
}
sort($list);

return $list;
}#-#GetFileList()

Use filectime()

 

function getFileList($directory) {
    $files = array();
    if (!is_dir($directory) || !is_readable($directory)) return $files;
    if ($handle = opendir($directory)) {
        while (false !== ($file = readdir($handle)) {
            if (preg_match('/\.(jpe?g|gif|png)$/i', $file)) {
                $fullpath = implode(DIRECTORY_SEPARATOR, array($directory, $file));
                $files[] = $fullpath;
            }
        }
        usort($files, create_function('$f1, $f2',
            '$f1ct = filectime($f1); $f2ct = filectime($f2); if ($f1ct === $f2ct) { return 0; } return $f1ct < $f2ct ? -1 : 1;'));
    }
    return $files;
}

 

Not sure this will work just wrote it out of the top of my head.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.