This code:
1) Goes through each directory and sub-directory (except those specifically excluded
2) Returns the name of only the .jpg files
What I also need it to do is:
1) Sort the file names by last modified date
2) Only list the 3 (or 4 or 5) newest file names
I'm pretty sure that I need to change the way the file name array is done, but really have no idea where to start.
Any ideas?
Thanks!
=================================
<?
// ASSIGN DIRECTORIES TO SCAN TO AN ARRAY
$DirectoriesToScan = array(realpath('.'));
// ASSIGN DIRECTORIES SCANNED TO AN ARRAY
$DirectoriesScanned = array();
// FUNCTION TO GET EXTENSION OF FILE NAMES
function get_extension($name)
{
$array = explode(".", $name);
$retval = strtolower(array_pop($array));
return $retval;
}
// LOOP THROUGH DIRECTORIES
while (count($DirectoriesToScan) > 0)
{
foreach ($DirectoriesToScan as $DirectoryKey => $startingdir)
{
[B]// OPEN THE DIRECTORY[/B]
if ($dir = @opendir($startingdir))
{
[B]// READ THE DIRECTORY[/B]
while (($file = readdir($dir)) !== false)
{
[B]// IF ENTRY IS NOT SELF, PARENT OR SELECTED DIRECTORIES (THUMBS & IMAGES), CONTINUE[/B]
if (($file != '.') && ($file != '..') && ($file != 'thumbs') && ($file != 'images'))
{
[B]// GET COMPLETE NAME OF ENTRY[/B]
$RealPathName = realpath($startingdir.'/'.$file);
[B]// ENTRY IS A DIRECTORY[/B]
if (is_dir($RealPathName))
{
[B]// ADD DIRECTORY NAME TO ARRAY[/B]
if (!in_array($RealPathName, $DirectoriesScanned) && !in_array($RealPathName, $DirectoriesToScan))
{
$DirectoriesToScan[] = $RealPathName;
}
}
[B]// ENTRY IS A FILE[/B]
elseif (is_file($RealPathName))
{
[B]// WE ONLY WANT JPGS, SO GET EXTENSION, THEN ADD ENTRY TO THE ARRAY IF IT MATCHES[/B]
if (get_extension($RealPathName)=="jpg")
{
$FilesInDir[] = $RealPathName;
}
}
}
}
[B]// CLOSE THE DIRECTORY[/B]
closedir($dir);
}
$DirectoriesScanned[] = $startingdir;
unset($DirectoriesToScan[$DirectoryKey]);
}
}
// REMOVE DUPLICATE VALUES FROM ARRAY - NOT NEEDED, KEEP FOR REFERENCE ONLY
//$FilesInDir = array_unique($FilesInDir);
// SORT FILES - NOT NEEDED, KEEP FOR REFERENCE ONLY
//sort($FilesInDir);
// CYCLE THROUGH FILES IN ARRAY AND ECHO EACH ONE WITH ITS LAST MODIFIED DATE
foreach ($FilesInDir as $filename)
{
echo date('m/d/Y h:ia',filemtime($filename)) . " " . $filename . "<BR>";
}
?>