kelsheikh Posted May 12, 2014 Share Posted May 12, 2014 Hi I just have a question about the caching code below. Should the date function be in the filename if I want to cache the file for several days? Or if the date changes will a new file be created and used instead of the the previous cache file. Is that not even used since my if statement checks for the file modification time? Thanks for the help in understanding this. $cachefile = 'cache/cached/'.$id . date('M-d-Y').'.php'; $cachetime = 172800; // Check if the cached file is still fresh. If it is, serve it up and exit. if (file_exists($cachefile) && time() - $cachetime < filemtime($cachefile)) { include($cachefile); exit; } ob_start(); {HTML TO CACHE} $fp = fopen($cachefile, 'w'); fwrite($fp, ob_get_contents()); fclose($fp); // finally send browser output ob_end_flush(); I basically want to cache a file for 2-3 days. Quote Link to comment https://forums.phpfreaks.com/topic/288448-php-file-caching-question/ Share on other sites More sharing options...
requinix Posted May 13, 2014 Share Posted May 13, 2014 The name of the file doesn't matter: && time() - $cachetime only the modification time. Quote Link to comment https://forums.phpfreaks.com/topic/288448-php-file-caching-question/#findComment-1479293 Share on other sites More sharing options...
kelsheikh Posted May 13, 2014 Author Share Posted May 13, 2014 Excellent, thanks so much requinix!! Quote Link to comment https://forums.phpfreaks.com/topic/288448-php-file-caching-question/#findComment-1479294 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.