Page 1 of 1

Sort a list of files by date modified

Posted: Tue Apr 06, 2004 7:31 pm
by Chris Corbyn
Hi,

I've got an array in which the elements are the filenames of all files in a directory. I then sort the array and echo the list back onto the page as links.

I wanted to sort the array according to how new the files are. I've tried using my code like this

Code: Select all

/*Function to get array of all files in directory*/

function listnew()
{
	$ToneArray = array();

/*Specifies the directory files are in*/
        
	$handle = opendir('latest');

/*Loops in directory to find files*/

	while ($file = readdir($handle))
	{

/*Reads the files without extensions then makes the array*/

		$tonefile = substr($file, 0, -4);
		$modified = filemtime($file);
				$ToneArray[$tonefile] = $modified;
	}

	return $ToneArray;
}

$ToneArray = listnew();

echo '<div align=left class="scroll" style="overflow:auto; width:300px; height:325px;">';
	echo '<p align=center><b><u>Latest Ringtones</u></b>';
	echo '<p class="test">';

/*Sort the Array*/

asort ($ToneArray);
$type = "newest";

/*Display Links for all the files*/ /*NB: $type was defined earlier in the script*/

foreach($ToneArray as $n => $v) { 
echo '<a href="index.php?location='.$type.'&n='.$n.'">'.$n.'</a><br>';

}

echo '</div>';
But the list is not in the order it should be in. I've tested it by creating new files in the directory but they are just positioned, apparently randomly to me in the list.

Is the modified date that you see when you right click and view file properties the same as what the script reads as the modified date or is my code wrong?

These files are not CHMOD to anything other than the standard 0755 but I can't see why this should make a difference.

Thanks

Posted: Tue Apr 06, 2004 9:03 pm
by Chris Corbyn
It's ok I've got it working.

It was just a stupid mistake.

I wasn't specifying the full path to the file in filemtime($file);

is should have done it like this

Code: Select all

filemtime('latest/'.$file.'');
Thanks anyway to all you other guys who would have pointed out my problem!