...

View Full Version : Resolved List files in a directory by time of modification.



fluxnuk3r
03-27-2009, 03:31 AM
I'm using the following script (found the original online and modified it) to list the files in a given directory with links to them, opening the files for display in an iframe. The problem is, I want to list the files newest to oldest, newest being at the top.


<?php

$folder = "files/";
$handle = opendir($folder);

# Making an array containing the files in the current directory:
while ($file = readdir($handle))
{
if ( $file != ".." && $file != "." )
{
$files[] = $file ;
}
}
closedir($handle);

#echo the files
foreach ($files as $file) {
echo "<a href=$folder$file target='read'>$file</a>"."<br />" ;
}
?>

If anyone could shed some light on how (and where) I might do this, it would be greatly appreciated.

oesxyl
03-27-2009, 04:01 AM
I'm using the following script (found the original online and modified it) to list the files in a given directory with links to them, opening the files for display in an iframe. The problem is, I want to list the files newest to oldest, newest being at the top.


<?php

$folder = "files/";
$handle = opendir($folder);

# Making an array containing the files in the current directory:
while ($file = readdir($handle))
{
if ( $file != ".." && $file != "." )
{
$files[] = $file ;
}
}
closedir($handle);

#echo the files
foreach ($files as $file) {
echo "<a href=$folder$file target='read'>$file</a>"."<br />" ;
}
?>

If anyone could shed some light on how (and where) I might do this, it would be greatly appreciated.
you can use filemtime to get the timestamp of last change, use it as key in the array and sort the array by keys using ksort.

http://www.php.net/manual/en/function.filemtime.php
http://www.php.net/manual/en/function.ksort.php

best regards

fluxnuk3r
03-27-2009, 05:46 AM
I read both pages you linked. Although I understand what they do, I don't understand how I can not only use them together, but how I can patch my script with them. I have tried using the filemtime function, but I haven't been using PHP long enough to understand where to put it in the script to make it work.

I am all for reading about the suggestions and trying to come to the answer, but I just don't know where to go with this. The example on the ksort() page does not really explain to me where to put the filemtime function as a key to ksort. Since my array is $files[] = $file, I am guessing that as opposed to the example shown, I won't use an array as a key, but the filemtype function. Which brings me back to the fact that I don't know how to do that.

Thanks again!

fluxnuk3r
03-27-2009, 06:49 AM
I've experimented a little with the functions. I did get some results, but not really what I wanted. Here is what it looks like now:


<?php
$folder = "files/";
$handle = opendir($folder);

# Making an array containing the files in the current directory:
while ($file = readdir($handle))
{
if ( $file != ".." && $file != "." )
{
$files[] = $file ;
}
}
closedir($handle);

#echo the files
ksort($files) ;
$key = filemtime();

foreach ($files as $file) {
echo "<a href=$folder$file target='read'>$file</a>"."<br />" ;
}
?>

ksort does sort the files, and it seemed at first as though it was sorting them by modification date (which didn't make sense to me, $key is defined, but is not used anywhere). I created 3 files in files/: "jnew", "new", and "znew". I used the first letters to make sure they were not sorted alphabetically. It listed the three files in descending order of modification, which was good. But, then I made the file "snew" and it was not placed at the top as it should have been. The order should be snew, znew, new, jnew. Instead, it became znew, jnew, snew, new. "snew", being the newest file, should be at the top. But it isn't.

I'm obviously going about this in the wrong way..

oesxyl
03-27-2009, 07:52 AM
- filemtime must have as argument the file
- ksort sort a array using keys and as I said the keys of your array must be the timestamp of the file. Try this:


<?php
$folder = "files/";
$handle = opendir($folder);

# Making an array containing the files in the current directory:
while ($file = readdir($handle)){
if( $file != ".." && $file != "." ){
$key = filemtime($file);
$files[$key] = $file ;
}
}
closedir($handle);
// sort files by mtime:
ksort($files) ;

#echo the files
foreach ($files as $file) {
echo "<a href=$folder$file target='read'>$file</a>"."<br />" ;
}
?>


best regards

kbluhm
03-27-2009, 01:40 PM
If you use the filemtime() value as the key, there will certainly be collisions on files that were last modified at the same time.

Have a look at array_multisort (http://www.php.net/array_multisort):


// grab the files
$files = glob( './folder/*.*' );

// re-order the files
$files = array_multisort(
// order by modified time, ascending
array_map( 'filemtime', $files ),
SORT_ASC,
SORT_NUMERIC,
// order by file name on similar times, ascending
array_map( 'basename', $files ),
SORT_ASC,
SORT_STRING,
// the array of files we are ordering
$files
);

This example is unested, but should get you on your way.

Nicklas
03-27-2009, 03:43 PM
Create an array containing the files and then use uasort() to sort it. Ex:

<?php

function newest($a, $b)
{
return (filemtime($a) > filemtime($b)) ? -1 : 1;
}

$dir = glob('files/*'); // put all files in an array
uasort($dir, "newest"); // sort the array by calling newest()

foreach($dir as $file)
{
echo '<a href="' . $file . '" target="read">' . basename($file) . '</a><br />';
}

?>

fluxnuk3r
03-27-2009, 05:18 PM
Thank you all for your help (its working now)...

oesxyl: the script did list the newest file, but only the newest file. the others somehow dissappeared. I added the new line and it only showed the one file. I thought that perhaps I had missed something so I copied the entire script and it still only returned one file.

kbluhm: thank you for your script as well. I was slightly confused by it and went on to Nicklas's post. If his hadn't worked I certainly would have tried to figure out how to use yours.

Nicklas: You did it! I'm not entirely sure how exactly, so I'll be investigating that. It lists all the files in the directory by modification time, newest to oldest. Thank you!

oesxyl
03-27-2009, 11:23 PM
Thank you all for your help (its working now)...

oesxyl: the script did list the newest file, but only the newest file. the others somehow dissappeared. I added the new line and it only showed the one file. I thought that perhaps I had missed something so I copied the entire script and it still only returned one file.

this is probably because of keys( see kbluhm post about collisions). I guess the simplest way to fix it is
to use filenames as keys, because are uniq, and timestamp as values, but this time you must use asort instead of ksort.

best regards

fluxnuk3r
03-27-2009, 11:52 PM
Never mind. The problem I had here was due to a design "oops".



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum