string cached_file_get_contents(string uri [, int ttl [, string path [, string prefix ]]]) -- Load a file from an URI, keeping a cached copy
<?php |
Published under the terms of the BSD License
<?php
function cached_file_get_contents($uri, $ttl = 900, $dir = '.', $prefix = '', $debug = false) {
$file = sprintf("%s/%s%s", $dir, $prefix, md5($uri));
if (!file_exists($file) || (time() - filemtime($file)) > $ttl) {
if (file_exists($file) && $debug !== false) printf("Getting %s (TTL:%d, Age:%d<br />\n", $uri, $ttl, time() - filemtime($file));
elseif ($debug !== false && !file_exists($file)) printf("Getting %s<br />\n", $uri);
$contents = file_get_contents($uri);
if ($contents === false) return false;
file_put_contents($file, $contents);
}
return file_get_contents($file);
}
?>
Last updated: Tue May 18 13:11:58 CEST 2010