note 110004 added to function.filemtime
| From: | bzplan at web dot de | Date: | Mon, 10 Sep 2012 21:28:54 +0000 |
| Subject: | note 110004 added to function.filemtime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-190614@lists.php.net to get a copy of this message | ||
quick $ dirty ... update_cache (file) function
<?php
// ------------------------------------------------------------------
### header("Content-Type: text/html; charset=utf-8");
// ------------------------------------------------------------------
function update_cache($source_url,$cache_file,$cache_time=0,$time_stamp=0) {
if (empty($cache_time)) { $cache_time = (3600); }
if (empty($time_stamp)) { $time_stamp = time(); }
if (!file_exists($cache_file)) {
copy($source_url,$cache_file);
$status = 'new';
}
else {
$fmtime = filemtime($cache_file);
$txdiff = ($time_stamp - $fmtime);
if ($txdiff >= $cache_time) {
copy($source_url,$cache_file);
$status = 'update';
}
else {
$status = 'cached';
}
}
return $status;
}
// ------------------------------------------------------------------
### example / usage:
// ------------------------------------------------------------------
// ------ set your parameters ------
$source_url = 'http://twitter.com/statuses/user_timeline/php_net.xml';
// ---- source URL here
$cache_path = 'cache/';
$cache_name = md5($sourceurl);
$cache_type = 'xml';
$cache_file = $cache_path.'_'.$cache_name.'.'.$cache_type;
// ------ use: $cache_time in seconds ------
// ... e.g. (60*10) = 10 minutes
// ... e.g. (3600*6) = 6 hours
// ... e.g. (3600*24*2) = 2 days
$cache_time = (3600*24);
$time_stamp = time();
// ------------------------------------------------------------------
// ------ call the function ------
$cache_status = update_cache($source_url, $cache_file, $cache_time, $time_stamp);
// ------------------------------------------------------------------
// ------ print the return ------
print "<br />\n";
print "<br />Cache Status: ".$cache_status."\n";
print "<br />\n";
// ------------------------------------------------------------------
// ------ now use the $cache_file to read from ------
$data = simplexml_load_file($cache_file, null, LIBXML_NOCDATA);
print "<pre>\n";
print_r ($data);
print "</pre>\n";
// ------------------------------------------------------------------
?>
... have fun !!! .. :=)
----
Server IP: 69.147.83.197
Probable Submitter: 188.193.218.26
----
Manual Page -- http://www.php.net/manual/en/function.filemtime.php
Edit -- https://master.php.net/note/edit/110004
Del: integrated -- https://master.php.net/note/delete/110004/integrated
Del: useless -- https://master.php.net/note/delete/110004/useless
Del: bad code -- https://master.php.net/note/delete/110004/bad+code
Del: spam -- https://master.php.net/note/delete/110004/spam
Del: non-english -- https://master.php.net/note/delete/110004/non-english
Del: in docs -- https://master.php.net/note/delete/110004/in+docs
Del: other reasons-- https://master.php.net/note/delete/110004
Reject -- https://master.php.net/note/reject/110004
Search -- https://master.php.net/manage/user-notes.php