Tuesday, August 16, 2011

PHP Code to get the recently updated file in a Directory.



<?php# Most recently updated file in a directory

# Set up
$DirectoryPath "YOURDIRECTORYPATH";$ExtPattern '\.(html|php|php4)$';
$NewTimeStamp 0;$NewFileName "";$DirConn opendir($DirectoryPath);
while (
$FileName readdir($DirConn)) {
        
# Eliminate current directory, parent directory
        
if (ereg('^\.{1,2}$',$FileName)) continue;
        
# Eliminate other pages not in pattern
        
if (! ereg($ExtPattern,$FileName)) continue;
        
$FileModifiedTime filemtime("$DirectoryPath/$FileName");
        if (
$FileModifiedTime $NewTimeStamp) {
                
$NewTimeStamp $FileModifiedTime;
                
$NewFileName $FileName;
                }
        }
# $NewTimeStamp is the time for the latest file
# $NewFileName is the name of the latest file
?><html>
<body>
<h2>Finding the most recent file in the DirectoryPath </h2>
<br />
... and it is  <?= $NewFileName ?>
which was modified last <?= date("l, j F Y",$NewTimeStamp?><br /></body>
</html>


No comments: