Page 1 of 1
Newest 5 files in directory
Posted: Mon Jun 17, 2002 3:14 am
by Dave
Hello,
I am trying to write a script that will read a list of .gif files from a directory and then display the last 5 to have been created.
Any help would be much appreciated - thanks in advance ~ Dave.
Posted: Mon Jun 17, 2002 3:33 am
by twigletmac
Posted: Mon Jun 17, 2002 4:35 am
by volka
in addition, this will sort the files (and directories) by their last modification time (descending)
Code: Select all
<html><body><table><?php
function mySort($a, $b)
{
return -strcmp($aї1], $bї1]);
}
$root = "./";
$handle = opendir($root) or die('cannot open directory');
$files = array();
while($file = readdir($handle))
{
if ($file != "." && $file != "..")
{
$absFile = $root.$file;
$sta = stat($absFile);
$filesї] = array($absFile, $staї'mtime']);
}
}
closedir($handle);
usort($files, 'mySort');
print_r($files);
?></table></body></html>
Second newest file only, third newest file etc..
Posted: Thu Jun 20, 2002 3:12 am
by Dave
Right, I have this working now, but am having trouble just returning a file of my choice - ie. returning the second newest file from the directory, and then the third through to the fifth newest file.
Again, any help would be great - thanks for reading

Posted: Thu Jun 20, 2002 6:21 am
by volka
the most recent modified file is stored at index [0] in the array, the second at [1] and so on. so i.e.
Code: Select all
for($i=0; $i<count($files) && $i < 5; $i++)
doFancyOutput($filesї$i]ї0]); // just the name (but with path)
will process the top 5 'most recent modified files' (hehe, just watched 'high fidelity'

)
I must be doing it wrong somewhere
Posted: Thu Jun 20, 2002 1:52 pm
by Dave
Still having problems with this, the code I'm using is..
Code: Select all
$numberOfFiles = sizeOf($fileArr);
for($t=0;$t<5;$t++)
{
$thisFile = each($fileArr);
$thisName = $thisFileї0];
$thisTime = $thisFileї1];
$thisTime = gmdate("F j, Y, g:ia", $thisTime+3600);
$file_size = filesize($thisName);
if ($file_size < '10000') { Header("Content-type: ");
readfile ( $dir_name . $fault ); } else {
Header("Content-type: ");
readfile ( $dir_name . $thisName ); }
}
How can I get the arrays from this - I keep getting odd results
Thanks for your help
Posted: Thu Jun 20, 2002 2:43 pm
by volka
what are you trying to do? header, readfile, uhhhh?
Probably you want to display those files, but 5 files each with it's own content-type is a little bit to much. And what shall the browser do with them?
You have to create a valid html-page first with img-tags for those files.
If the directory is public and relative to the script's path you may use this
Code: Select all
<html><body><table><?php
function mySort($a, $b)
{
return -strcmp($aї1], $bї1]);
}
$root = "imgdir";
$handle = opendir($root) or die('cannot open directory');
$files = array();
while($file = readdir($handle))
{
if ($file != "." && $file != "..")
{
$absFile = $root.$file;
$sta = stat($absFile);
$filesї] = array($absFile, $staї'mtime'], $staї'size']);
}
}
closedir($handle);
usort($files, 'mySort');
foreach($files as $file)
{
print('<tr><td><img alt="'.$fileї0].'" src="');
if ($fileї2] < 10000)
print('faulty.gif');
else
print($root);
print('"></td><td>'.$fileї1].'</td><td>'.$fileї2].'</td></tr>');
}
?></table></body></html>
(doesn't check for valid image-type at all)
btw. does anybody know how to access apache's mime-type-library from within php?
what I'm trying to do
Posted: Thu Jun 20, 2002 2:51 pm
by Dave
Basically I'm working on having 5 copies of this file, each one for the last 5 new files (in this case image files).
I have directly linked them at the moment and have the page working it out so I have the latest 5, but I don't want people to link to the image direct, which is why I'm generating it through the script so that I can also add an ip check and the error check, should the image be broken (size).
Posted: Fri Jun 21, 2002 10:20 am
by will
expanding on volka's post a little (i think this may be more what you need)....
pass recent_pics() a directory (relative to root) and an array of what images you want (this would also work if you ever want to go beyond the most recent 5 to say 10 or 15).
edit the echo in the foreach($list) loop to do whatever you want to do with the pictures (it currently just prints out the name).
Code: Select all
<?php
recent_pics("images/",array(1,2));
function recent_pics($root,$list) {
$handle = opendir($root) or die('cannot open directory');
$files = array();
while($file = readdir($handle))
{
if ($file != "." && $file != ".." && ereg("((jpg)|(gif))$",$file))
{
$absFile = $root.$file;
$sta = stat($absFile);
$filesї] = array($absFile, $staї'mtime'], $staї'size']);
}
}
closedir($handle);
usort($files, 'mySort');
foreach($list as $item) {
echo $filesї$item-1]ї0].'<br>';
}
}
function mySort($a, $b)
{
return -strcmp($aї1], $bї1]);
}
?>
Posted: Tue Jun 25, 2002 7:54 am
by volka