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
https://forums.phpfreaks.com/topic/209659-how-to-sort-array-with-files-on-date/
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

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?

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?

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.