pea_1 Posted March 15, 2007 Share Posted March 15, 2007 Hello, i'm trying to find the last modified file in a set of directories but i can't figure out how to compare the file date, pick the newest date and then displaying the file name. I have this, but i'm not sure where to go or if it's even the way to do it. Thanks, Peter. <?php //put each dir into an array $dir = "../forum/"; $filearray = array(); if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { if(preg_match("/forumposts_/", $file)){ array_push($filearray, "$file"); } } closedir($dh); } } //echo each file and it's last modify date foreach($filearray as $filer){ $filer = "../forum/$filer"; if ($handle = opendir($filer)) { while (false !== ($file = readdir($handle))) { if (preg_match("/.php/", $file)) { $file = "$filer/$file"; echo "$file".date ("F d Y H:i:s.", filemtime($file))."<br>"; } } closedir($handle); } } ?> Link to comment https://forums.phpfreaks.com/topic/42891-finding-the-newest-file/ Share on other sites More sharing options...
boo_lolly Posted March 15, 2007 Share Posted March 15, 2007 you may get some use out of touch() Link to comment https://forums.phpfreaks.com/topic/42891-finding-the-newest-file/#findComment-208383 Share on other sites More sharing options...
pea_1 Posted March 15, 2007 Author Share Posted March 15, 2007 Ah thanks. I think i can use the script from this comment: Neat little script that will give you a list of all modified files in a certain folder after a certain date: $filelist = Array(); $filelist = list_dir("d:\\my_folder"); for($i=0;$i<count($filelist);$i++){ $test = Array(); $test = explode("/",date("m/d/Y",filemtime($filelist[$i]))); //example of files that are later then //06/17/2002 if(($test[2] > 2001) && ($test[1] > 16) && ($test[0] > 5)){ echo $filelist[$i]."\r\n"; } clearstatcache(); } function list_dir($dn){ if($dn[strlen($dn)-1] != '\\') $dn.='\\'; static $ra = array(); $handle = opendir($dn); while($fn = readdir($handle)){ if($fn == '.' || $fn == '..') continue; if(is_dir($dn.$fn)) list_dir($dn.$fn.'\\'); else $ra[] = $dn.$fn; } closedir($handle); return $ra; } Link to comment https://forums.phpfreaks.com/topic/42891-finding-the-newest-file/#findComment-208401 Share on other sites More sharing options...
pea_1 Posted March 15, 2007 Author Share Posted March 15, 2007 Now i can't find anything on sorting by date.. You might be able to make your own sorter, not that i know how, but even then it must be a huge script. Is there any other way? Link to comment https://forums.phpfreaks.com/topic/42891-finding-the-newest-file/#findComment-208427 Share on other sites More sharing options...
boo_lolly Posted March 15, 2007 Share Posted March 15, 2007 i'd write you an example, but i simply do not have the time. but to explain what i'd do, is write something like a foreach loop that checks the last time each file was accessed. then, pump each file into the correct index in order. so if the first file it reads was accessed 3rd, it would store it in the 3rd index of the array. does that make sense? Link to comment https://forums.phpfreaks.com/topic/42891-finding-the-newest-file/#findComment-208441 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.