开发者

file expiry check not working

开发者 https://www.devze.com 2023-03-19 03:07 出处:网络
I\'m having a problem. I\'ve got a file that I only want to update on a daily basis. So I\'m using filemtime and an expiry timestamp and checking them against each other...problem is I\'m getting t开发

I'm having a problem. I've got a file that I only want to update on a daily basis. So I'm using filemtime and an expiry timestamp and checking them against each other...problem is I'm getting t开发者_JS百科he echo statement every time I'm refreshing the page. Any ideas?

$time_expire = time() + 24*60*60; // Expire Time (1 Day)
// Check file change time
if(filemtime($cache_filename) <= $time_expire) {
    echo 'file is too old - refresh it'; 
}


Just think through the logic slowly:

  • expiry timestamp is one day in the future from now
  • check if the modification timestamp is lower than or equal to the expiry timestamp

Yes, I think that will always be true. Unless your file is From The Future™.
Your expiry timestamp has to be a day in the past.


It is because your filemtime is in the past (at least should be) and you compare it to future ( time() + 24*60*60 ). Of course, it is less.

Get some coffee and rethink what you've coded there ;-)

A "little" tip:

if ( time() - filemtime($cache_filename) >= 3600 * 24) ) { /* file is too old, lets refresh it */ }


Suppose if your filemtime is 1000000 and current time() is 1000060 your time_expire = 1000060 + 86400. So your condition is always true

Instead you need to check filemtime + 86400 <= time()

0

精彩评论

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

关注公众号