I am running a program in which part of it iterates through a directory and stores the most recent date in a variable and the name of the most recent folder in another. I have a series of alerts set up so that i can debug.
Alerts Order:
- Expresses current sub-directory and time
- Then the variable with the subdirectory date dubbed "temp" and latest date .
- Then "update time" if temp is more recent then the "latest"
- Then "Time changed" if temp is more recent then the "latest"
- Then it expresses the new latest time and latest folder name associated with that date
My problem is through the first subdirectory this all behaves normal. Then the second directory it gets messed up. The first 2 alerts are expected (the second directory is older than the first by the way). The third and fourth alerts are skipped (as expected). but the 5th alert shows that the latest folder variable has been changed to the current subdirectory (yet the latest time remains with the past iterated subdirectory time).
hope this makes sense....here is the code
<?php
$files = array();
$latestTime = date("1900-01-01"); ///older then any of the folders will be
$latestFolder = "none";
foreach (new DirectoryIterator('./images/ISGC_images/') as $fileInfo) { ///iterate through directory
if($fileInfo =="."|$fileInfo == "..") continue;
if($fileInfo->isDir()) {
echo "<script type='text/javascript'>alert('".$fileInfo." was updated ".date("F d Y H:i:s.",filemtime('./images/ISGC_images/'.$fileInfo))."');</script>";
$tempDate = date("F d Y H:i:s.",filemtime('./images/ISGC_images/'.$fileInfo));
echo "<script type='text/javascript'>alert('"."temp time is ".$tempDate.'and latest time is'.$latestTime."');</script>";
if ($tempDate > $latestTime)
echo "<script type='text/javascript'>alert('"."update time"."');</script>";
if ($tempDate > $latestTime) {
$latestFolder = $fileInfo;
$latestTime = $tempDate;
echo "<script type='text/javascript'>alert('"."Time Changed!"."');</script>";
}
echo "<script type='text/javascript'>alert('"."latest folder is ".$latestFolder."');</script>";
echo "<script type='text/javascript'>alert('"."latest time is ".$latestTime."');</script>";
}
}
?>
ANSWER
Line 14 needs to be changed to $latestFolder = (string)$fileInfo; because a folder object cannot be stored in a variable apparently