help about file last modified

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
zerodegreeburn
Forum Newbie
Posts: 24
Joined: Thu Jul 04, 2002 6:42 am
Contact:

help about file last modified

Post 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?

:)
User avatar
musashi
Forum Commoner
Posts: 39
Joined: Tue Jul 23, 2002 12:51 pm
Location: Santa Cruz - CA

Find the file

Post 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)
zerodegreeburn
Forum Newbie
Posts: 24
Joined: Thu Jul 04, 2002 6:42 am
Contact:

Post 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
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
zerodegreeburn
Forum Newbie
Posts: 24
Joined: Thu Jul 04, 2002 6:42 am
Contact:

Post 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.
Rob the R
Forum Contributor
Posts: 128
Joined: Wed Nov 06, 2002 2:25 pm
Location: Houston

Post 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); 
?>
zerodegreeburn
Forum Newbie
Posts: 24
Joined: Thu Jul 04, 2002 6:42 am
Contact:

Post by zerodegreeburn »

:oops:

i did actually try this...


thanks
zerodegreeburn
Forum Newbie
Posts: 24
Joined: Thu Jul 04, 2002 6:42 am
Contact:

Post 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.
Rob the R
Forum Contributor
Posts: 128
Joined: Wed Nov 06, 2002 2:25 pm
Location: Houston

Post 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.
zerodegreeburn
Forum Newbie
Posts: 24
Joined: Thu Jul 04, 2002 6:42 am
Contact:

Post by zerodegreeburn »

and...

yep, thanks it works :D
Post Reply