开发者

Filemtime/cachetime where am I going wrong?

开发者 https://www.devze.com 2023-01-06 12:32 出处:网络
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....

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


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


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
}

?>


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())) {


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
}
?>
0

精彩评论

暂无评论...
验证码 换一张
取 消