Jump to content

How to sort array with files on date


OrangeTux

Recommended Posts

Hello,

 

I'm making a imagegallery and when someone upload a new image that image has to appear as first picture of the gallery.

I'm using an array for the files, but how can I sort the files on date, newest first.?

<?php
$n = 0;
$pathToThumbs = "images/tekeningen/";
$dir = opendir( $pathToThumbs );
while (false !== ($fname = readdir($dir))){
	    
	    // strip the . and .. entries out
    	if ($fname != '.' && $fname != '..'){
    		$tekeningen[$n] = $fname;
   	} echo $tekeningen[$n]."<br />"; 
   	 $n = $n +1;
}
   closedir( $dir );
?>

 

I found solutions with ksort and filemtime, but I don't understand them and/or they didn't work for me.

 

Link to comment
Share on other sites

To be able to sort the filenames by date you have to store the names and dates in an array, sort the array and then display the information on the screen, for example:

<?php
$pathToThumbs = "images/tekeningen/";
$f = glob($pathToThumbs . '/*');
$tmp = array();
foreach ($f as $file) {
   $tmp[$file] = filemtime($file);
}
arsort($tmp);
foreach ($tmp as $file => $time) {
   echo basename($file) . "<br>\n";
}
?>

 

Ken

Link to comment
Share on other sites

It works half. I'm on a Linux system, btw, I don't know if that is a part of the problem.

 

When I copy existing file's to the folder, the files are sorted random. They aren't sorted on physical place.

But when I create new files in that folder these file's are sorted correct.

 

What can be the problem?

Link to comment
Share on other sites

I'm using the script that you've posted above.

-+But I know already the problem.-

 

When I copy an existing file to another folder the modification date won't change.

 

e.g.

x.txt is last modificated at 14:45 04-08-2010.

I copy x.txt to another folder at 17:00 04-08-2010.

x.txt in the new folder is still last modificited at 14:45 04-08-2010, not at 17:00 04-08-2010.

 

Is there a way I can fix this or can do a trick to solve the problem?

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.