picea73
12-14-2008, 05:10 PM
Hi,
is anyone aware of any php scripts currently available that would allow me to search a folder containing a number of subfolders and then display the title (less extension) of the newest file in the combined directories (with a link to the file in question)? So far I've only come across ones that allow searching of single, specified directories.
Essentially, we have a series of folders of church notice sheets as pdfs and I'd like our home page to have a link to the latest one (current structure is year/month/file.pdf)
Many thanks.
abduraooft
12-15-2008, 09:11 AM
Have a look at http://webxadmin.free.fr/article/php-recursive-dir-and-sort-by-date-344.php
picea73
12-15-2008, 11:21 AM
Works great - thanks very much!
Without sounding cheeky, how would I convert the code to only display the latest entry?
Thanks again.
abduraooft
12-15-2008, 11:48 AM
Try echo $Files[count($Files)-1]; at the end.
picea73
12-15-2008, 12:02 PM
all I get it the word "Array" printed out.
Code I'm using (prior to the above) is:
<?php
function LoadFiles($dir,$filter="")
{
$Files = array();
$It = opendir($dir);
if (! $It)
die('Cannot list files for ' . $dir);
while ($Filename = readdir($It))
{
if ($Filename != '.' && $Filename != '..' )
{
if(is_dir($dir . $Filename))
{
$Files = array_merge($Files, LoadFiles($dir . $Filename.'/'));
}
else
if ($filter=="" || preg_match( $filter, $Filename ) )
{
$LastModified = filemtime($dir . $Filename);
$Files[] = array($dir .$Filename, $LastModified);
}
else
continue;
}
}
return $Files;
}
function DateCmp($a, $b)
{
return strnatcasecmp($a[1] , $b[1]) ;
}
function SortByDate(&$Files)
{
usort($Files, 'DateCmp');
}
$Files = LoadFiles("documents/Noticesheet/");
SortByDate($Files);
reset($Files);
while (list($k,$v) =each($Files))
{
echo "<a href='";
?><?=$v[0]?><?
echo "'>Latest Weekly Noticesheet";
echo "</a><br>";
}
?>
Fou-Lu
12-15-2008, 12:07 PM
'Tis an array.
Depends on you're ordering (I'm too tired to figure it out from code reading, yeesh :P)
You'll either use: $aFilesArray[0] or $aFilesArray[$i] where $i = count($aFilesArray) - 1 (depending on if its asc sorted or desc sorte).
I swear that I posted a solution for you on this earlier today. Mine was simplier :)
abduraooft
12-15-2008, 12:13 PM
Oh.. my bad, I hadn't tested.
echo $Files[count($Files)-1][0];
(count(), not strlen() :-( )
picea73
12-15-2008, 12:18 PM
Great - works fantastic! Thanks a lot for the help.
For anyone else reading, the final code is:
<?php
function LoadFiles($dir,$filter="")
{
$Files = array();
$It = opendir($dir);
if (! $It)
die('Cannot list files for ' . $dir);
while ($Filename = readdir($It))
{
if ($Filename != '.' && $Filename != '..' )
{
if(is_dir($dir . $Filename))
{
$Files = array_merge($Files, LoadFiles($dir . $Filename.'/'));
}
else
if ($filter=="" || preg_match( $filter, $Filename ) )
{
$LastModified = filemtime($dir . $Filename);
$Files[] = array($dir .$Filename, $LastModified);
}
else
continue;
}
}
return $Files;
}
function DateCmp($a, $b)
{
return strnatcasecmp($a[1] , $b[1]) ;
}
function SortByDate(&$Files)
{
usort($Files, 'DateCmp');
}
$Files = LoadFiles("documents/Noticesheet/");
SortByDate($Files);
echo " <a href='";
echo $Files[count($Files)-1][0];
echo "'>Latest Weekly Noticesheet";
echo "</a><br>";
?>