Jump to content

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


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.