Jump to content

Finding the newest file


pea_1

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.