Jump to content

Sort images by date


yengec

Recommended Posts

There are two different codes. both works. but how will sort images by date? Where and how do I use filemtime. Thank you.

 

First Code:

$scandir_array = scandir('files');
foreach($scandir_array as $folder){
	if(is_dir('files/'.$folder) and $folder!='.' and $folder!='..'){
		// define this key in the array, it will be blank, store categories as keys
		$categories_array[$folder] = array();
		// $total_photos_array[$folder] = 0;
		$files_in_dir = scandir('files/'.$folder);
		foreach($files_in_dir as $file){
			if($file!='.' and $file!='..'){
				// if file is not the category thumbnail (thumbnail.jpg) and not _thumb.jpg and not _small.jpg
				if($file != "thumbnail.jpg" and substr($file, strlen($file)-10) != "_small.jpg" and substr($file, strlen($file)-10) != "_thumb.jpg"){
					// $total_photos_array[$folder]++;
					$base_file_name = substr($file, 0,  strlen($file)-4);
					// insert this file in the array of files
					array_push($categories_array[$folder], $base_file_name);
					
				}
			}
		}
	}
}
krsort($categories_array);

Second Code:

if ($handle = opendir('files')) {
    while (false !== ($folder = readdir($handle))) {
		if(is_dir('files/'.$folder) and $folder!='.' and $folder!='..'){
			
			// define this key in the array, it will be blank, store categories as keys
			$categories_array[$folder] = array();

			// $total_photos_array[$folder] = 0;
			
			$files_in_dir = scandir('files/'.$folder);
			foreach($files_in_dir as $file){
				// if file ends in _thumb.jpg
				if(strpos($file, '_thumb.jpg') === strlen($file)-10){
					// $total_photos_array[$folder]++;
					$base_file_name = substr($file, 0,  strlen($file)-10);
					// insert this file in the array of files
					array_push($categories_array[$folder], $base_file_name);
				}
			}
			
		}
    }
    closedir($handle);
}
krsort($categories_array);

Link to comment
Share on other sites

Instead of having $categories_array[$folder] be an array of filenames, have it be an array of filenames => modification times. That's where you use filemtime().

$categories_array[$folder][$base_file_name] = filemtime('files/' . $folder . '/' . $file);
After the files loop finishes (and before you move onto the next folder), asort the $categories_array[$folder] array.
Link to comment
Share on other sites

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.