2

What I am trying to do is check the age of an image, if its older than 60 minutes then run another php page to fetch images otherwise do nothing if less than 60 minutes old....

The script isn't opening the 2nd page (radartest.php) and running it to update the images, so need a command to tell it to run as a script please.

<?php
$imagename='./radar/auck0.png';
$cachetime = 3600;
    //Start the caching of the images........
    if (file_exists($imagename) and filemtime($imagename) + $cachetime > time()) {
          echo("radartest.php");
        } else {
  null; //do nothing
}
?>
Gary
  • 21
  • 1
  • 3

4 Answers4

5

Result returned by filemtime is cached.

It's only a guess, but if you're using this piece of code too frequently you may have to use the clearstatcache function : http://php.net/manual/en/function.clearstatcache.php

syl.fabre
  • 696
  • 1
  • 7
  • 20
1

Also you're using echo('radartest.php'); shouldn't that be include('radartest.php'); ?

ie.

<?php
$imagename='./radar/auck0.png';
$cachetime = 3600;
    //Start the caching of the images........
    if (file_exists($imagename) && (filemtime($imagename) + $cachetime) > time()) {
          include ("radartest.php");
        } else {
  null; //do nothing
}

?>

Kieran Allen
  • 961
  • 5
  • 6
  • Have tried require_once, echo, print and include but not triggering the 2nd page at all, also dropped cachetime down to 10 seconds for testing purposes. – Gary Jul 10 '10 at 17:51
0

Not sure if it could be operator precedence. You're using "and" which is lower precedence than && Try bracketing your expressions to force precedence:

if ((file_exists($imagename)) && ((filemtime($imagename) + $cachetime) > time())) {
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • This is not what the problem is. `&&` and `and` have the same precedence relative to `+` and `>`. http://php.net/manual/en/language.operators.precedence.php – Hammerite Jul 10 '10 at 11:44
  • I just always get concerned when I see and rather than &&. A debug test should display the filemtime() value so that the poster could do a manual comparison of the values. I've found that on some systems, mtime isn't always reliable – Mark Baker Jul 10 '10 at 11:50
  • I have changed the complete line, embarrassing thing is that I was checking the image age on the webpage instead of on the server, to my astonishment, I found I'm not actually triggering the 2nd page at all. – Gary Jul 10 '10 at 17:50
0

Seems to work well....

<?php
$cache_file = './radar/auck0.png';
$cache_life = '3600'; //caching time, in seconds
$filemtime = @filemtime($cache_file);  // returns FALSE if file does not exist
if (!$filemtime or (time() - $filemtime >= $cache_life)){
          include("wxrain.php");
        } else {
        null; //do nothing
}
?>
Gary
  • 21
  • 1
  • 3