Page 1 of 1

help about file last modified

Posted: Tue Jul 23, 2002 4:12 pm
by zerodegreeburn
on my site i have a flat-file counter system for a section of games, it just stored the 3bytes of info needed for game played "223" times, you get the idea.

what i would like to do is create a script that finds which counter file was last modified (written too), so that i could say

"$game was last played ....."

except i have not very much idea how to do this, lol, i know you can find out the filemtime thing but i dunno how to work that on a file that your not running it from....


anyheads up?

:)

Find the file

Posted: Tue Jul 23, 2002 5:43 pm
by musashi
So what I think is the best approach is to simply iterate through the directory that contains all the counter files... if you don't have this, possibly consider either a different organization or create links to each file in a single directory.

Then we could do this:

Code: Select all

$path = "/path/to/counter/dir";

$counter_dir = opendir($path);
while($file=readdir($counter_dir)) {
  if(is_file("$path/$file")){
     if($last_changed_time < filemtime("$path/$file")) &#123;
        $last_changed_time = filemtime("$path/$file");
        $last_changed = $file;
     &#125; 
  &#125;
&#125;
closedir($counter_dir);
Now $last_changed will always equal the last file that was modified. (plus you can get the unix time stamp if you wanted too for when it was accessed. Using the date function you could nicely format that)

Posted: Wed Jul 24, 2002 4:38 am
by zerodegreeburn
how would i get the unix time :?

i really dont know, i got that to work so far,,,

http://www.zerodegreeburn.com/games.php
(Games - The last game played was Space Invaders)
obviously what would be fun to add was
(Games - The last game played was Space Invaders on Wed 24 10:26:23)

.... i feel stuuupid, i been looking around at file last modified but i still dont get how to do it on a file that your not directly ON.

echo "Last modified: " . date( "D d H:i:s", getlastmod() );

that

Posted: Wed Jul 24, 2002 6:26 am
by twigletmac
filemtime() returns a Unix timestamp. To format that timestamp you can use date() like you had in your post, just with $last_changed_time as the second argument:

Code: Select all

$date_info = date('D d H:i:s', $last_changed_time);
Mac

Posted: Wed Dec 11, 2002 9:28 am
by zerodegreeburn
sorry to bring up an old topic but...

how would i get the last counter file that hadnt been modified for longest.

would you have to count the number of files in the dir and keep going back? that seems a little long, there must be a better way.

Posted: Wed Dec 11, 2002 9:45 am
by Rob the R
You can use the same code that Mushashi provided, just keep track of the oldest file instead of (or in addition to) the most recent file:

Code: Select all

<?php
$path = "/path/to/counter/dir"; 

$counter_dir = opendir($path); 
while($file=readdir($counter_dir)) { 
  if(is_file("$path/$file")){ 
     // finds most recent file
     if($newest_time < filemtime("$path/$file")) { 
        $newest_time = filemtime("$path/$file"); 
        $newest_file= $file; 
     }
     // finds oldest file - only change is the ">" character
     if($oldest_time > filemtime("$path/$file")) { 
        $oldest_time = filemtime("$path/$file"); 
        $oldest_file = $file; 
     } 
  } 
} 
closedir($counter_dir); 
?>

Posted: Wed Dec 11, 2002 9:49 am
by zerodegreeburn
:oops:

i did actually try this...


thanks

Posted: Wed Dec 11, 2002 9:56 am
by zerodegreeburn
just doesnt display it..?

variables are empty when i try that (its what happened when I tried it)

code:

Code: Select all

<?php 

$counter_dir = opendir($path);
while($file=readdir($counter_dir)) {
  if(is_file("$path/$file")){
	  //finds most recent file
     if($last_changed_time < filemtime("$path/$file")) {
        $last_changed_time = filemtime("$path/$file");
        $last_changed = $file;
		putenv( "TZ=Europe/London");
		$modified = date("F d, H:i:s a", filemtime("$path/$file") );
	 }
	//finds oldest file
     if($oldest_time > filemtime("$path/$file")) {
        $oldest_time = filemtime("$path/$file");
        $oldest_file = $file;
		putenv( "TZ=Europe/London");
		$oldest_modified = date("F d, H:i:s a", filemtime("$path/$file") );
	 }
  }
}
closedir($counter_dir); 


echo "$last_changed - <b>$modified</b><br />";
echo "$loldest_file - <b>$oldest_modified</b>";

?>
this prints out the first echo fine, but the second is just blank variables.

Posted: Wed Dec 11, 2002 10:50 am
by Rob the R
Ah. OK. That's what I get for posting code I haven't tested. It looks like you'll need to initialize the $oldest_time variable so that the first file encountered will immediately be older than the time stored in $oldest_time. Try adding:

Code: Select all

$oldest_time = time() ;
before the "while" loop. That way, the very first file through the loop (at least) should be older than $oldest_time and it should go into that if clause.

It looks like you also have a typo in your last "echo"statement. There is an extra "l" in your variable name. No big deal.

Posted: Wed Dec 11, 2002 10:53 am
by zerodegreeburn
and...

yep, thanks it works :D