1

Can I use filemtime in PHP to force a browser to reload a css?

 $mtime = filemtime('style.css');
 echo "\style.css?mod=" . $mtime;

Is filemtime too slow?

chrvadala
  • 582
  • 6
  • 17
  • 2
    I was looking at using `filemtime()` as part of a browser cache busting technique for css/js/image files and was considering whether caching the `mtime` using `memcached` would be beneficial to prevent multiple file system calls. 1000 `filemtime()` calls (including clearing the stat cache between calls) vs. 1000 `memcached` reads. `filemtime()` is significantly faster. 1000 `filemtime()` took: 0.004787 1000 `memcache` took: 0.075099 – user1909426 Jan 03 '13 at 17:36
  • doesn't have to be mtime. just a DIFFERENT value than what was used previously, so the browser will see a "new" url and force a load. – Marc B Jan 03 '13 at 17:37

1 Answers1

5

filemtime should be fine under normal circumstances.

You could also look into E-Tags and just have Apache do the work for you.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
  • `filemtime` returns timestamp, which is increased every second. – nothrow Jan 03 '13 at 17:36
  • 1
    Apache has E-Tags enabled by default (I see E-tag in response header), but when I update my stylesheet usually the browser load it from cache instead of server. I think that use a param in the link is a strong method to force the browser to avoid cache. My problem is how calculate it avoiding overload. – chrvadala Jan 03 '13 at 18:26
  • @Yossarian nope. You are mistaken with time(). http://www.php.net/manual/en/function.filemtime.php – aur0n May 28 '14 at 15:34
  • On an SSD based server with operating system file system caching for frequently accessed resources, the overhead for `filemtime` is likely to be very low, at least for smaller sites, though this is only anecdotal based on a site experiencing ~1,000 page views a day. The small amount of time spent accessing file modification times is made up in spades by other website caching mechanisms. – Nick Bedford Jan 04 '17 at 07:00