Jump to content

Recommended Posts

What is the best way to sort images in a folder by date. Here's what i've got so far:

 

	$images = array();//set array ready for the images
	$handle = opendir($imagesFolder);//open directory handle

	while(false !== $fName = readdir($handle)){
		//get file path
		$fileInfo = pathinfo($imagesFolder.$fName);			
		//get and convert extension to lower case
		$extension = strtolower($fileInfo['extension']);

		//add jpg and png file types to array
		if($extension == 'jpg' || $extension == 'jpeg'){
			$images[] = $fName;
		}
		if($extension == 'png'){
			$images[] = $fName;
		}
	}

	$sortedImages = array();

	foreach($images AS $image){
		$fileTime =  filemtime($imagesFolder . $image);//get image creation time
		$sortedImages[] = array($fileTime => $image);
	}

	sort($sortedImages);
		foreach($sortedImages AS $sorted){
			foreach($sorted AS $key => $value){
				echo "<strong>Filetime:</strong> " . $key . " <strong>Filename:</strong> " . $value . "<br />";
			}
		}

 

All works well apart from the sorting part. It does sort something, but i'm assuming its just the size of the number. I didn't expect sort() to work but it was worth a shot.

 

This is what the above script outputs:

 

Filetime: 1268932652 Filename: 1.png

Filetime: 1261508122 Filename: 5.jpg

Filetime: 1268933066 Filename: 2.png

Filetime: 1268932936 Filename: 4.png

Filetime: 1268957350 Filename: 3.png

Filetime: 1261508126 Filename: 6.jpg

 

The files are created respective of their names. e.g 1.png was created first, 2.png was created second and so on.

 

How can i sort these based on the time they were created?

 

Thanks.

function getFilesByExtension($directory, $validExtensions) {
  $files = array();
  $directory = rtrim($directory, '\/') . DIRECTORY_SEPARATOR;
  $dh = opendir($imagesFolder);
  while (false != ($file = readdir($dh))) {
    $extension = pathinfo($file, PATHINFO_EXTENSION);
    $extension = strtolower($extension);
    if (in_array($extension, $validExtensions)) {
      $files[] = $directory . $file;
    }
  }
  closedir($dh);
  return $files;
}

function usort_sortFilesByLastModifiedTimeHelper($file1, $file2) {
  $mtime1 = filemtime($file1);
  $mtime2 = filemtime($file2);
  
  if ($mtime1 === $mtime2) {
    return 0;
  }
  return $mtime1 < $mtime2 ? -1 : 1;
}

$images = getFilesByExtension($imagesFolder, array('jpg', 'jpeg', 'gif', 'png'));
usort($images, 'usort_sortFilesByLastModifiedTimeHelper');
print_r($images);

Thanks! I'll give it a go. Just trying to work everything out first before i attempt to write a function (i would reuse your code but i would rather know what's going on).

 

I think i understand it all apart from this bit:

 

  $directory = rtrim($directory, '\/') . DIRECTORY_SEPARATOR;

 

What's with the DIRECTORY_SEPARATOR bit? I've never seen that done before, wont it just add that onto the end of the $directory string?

What's with the DIRECTORY_SEPARATOR bit? I've never seen that done before, wont it just add that onto the end of the $directory string?

 

Indeed it does.

 

$directory = rtrim($directory, '\/') . DIRECTORY_SEPARATOR;

 

rtrim first removes the trailing slash if any and adds a trailing slash. This code makes sure that $directory contains a trailing slash before proceeding.

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.