note 40601 added to function.filemtime
| From: | bemi at users dot sourceforge dot net | Date: | Tue, 09 Mar 2004 11:22:29 +0000 |
| Subject: | note 40601 added to function.filemtime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-66481@lists.php.net to get a copy of this message | ||
Be warned that depending on your hosting service, it may happen that files created by touch () and
by fopen () may not have the same filemtime () and/or filectime. See below for some test code.
Also, when a file is uploaded through FTP, its creation/modification date may be
"fopen-like" or "touch-like"... You will have to test.
This could lead to trouble for cache systems (quite annoying bugs, hard to find and long to fix and
test) or other time-dependant code (importing files if more recent than some other, etc.).
Finally, I've seen new files having different file times depending on their path...
Specifically, one file created in my web directory and another one in a temp directory (using
tempnam ()) have some 12 minutes file time difference!
And now for some test code :
<?
function showFileTimesAndUnlink ($fileName) {
clearstatcache ();
echo 'filemtime : ' . date ('r', filemtime ($fileName)) . '<br
/>';
echo 'filectime : ' . date ('r', filectime ($fileName)) . '<br
/>';
unlink ($fileName);
}
function showTimes ($tmpFileName) {
echo 'Touch file : ' . $tmpFileName . '<br />';
touch ($tmpFileName);
showFileTimesAndUnlink ($tmpFileName);
echo 'Fopen file : ' . $tmpFileName . '<br />';
$f = fopen ($tmpFileName, 'w+');
fclose ($f);
showFileTimesAndUnlink ($tmpFileName);
}
echo 'Date : ' . date ('r') . '<br />';
$tmpFileName = tempnam ($_SERVER['DOCUMENT_ROOT'], '');
showTimes ($tmpFileName);
$tmpFileName = tempnam ('', '');
showTimes ($tmpFileName);
?>
..giving on my host the following result :
Date : Tue, 9 Mar 2004 12:12:43 +0100
Touch file : /home/site/www/qpTkZH
filemtime : Tue, 9 Mar 2004 12:12:43 +0100
filectime : Tue, 9 Mar 2004 11:59:46 +0100
Fopen file : /home/site/www/qpTkZH
filemtime : Tue, 9 Mar 2004 11:59:46 +0100
filectime : Tue, 9 Mar 2004 11:59:46 +0100
Touch file : /tmp/sC0ZUU
filemtime : Tue, 9 Mar 2004 12:12:43 +0100
filectime : Tue, 9 Mar 2004 12:12:43 +0100
Fopen file : /tmp/sC0ZUU
filemtime : Tue, 9 Mar 2004 12:12:43 +0100
filectime : Tue, 9 Mar 2004 12:12:43 +0100
Workaround : you may touch () all the files you're going to check using filmtime after
they've been updated using fopen (). Or you may use some custom touchFile () function instead
of touch ().
Here's mine : it opens the file, appends nothing to it, and closes it.
<?
function touchFile ($fileName) {
$f = fopen ($fileName, 'a+');
fclose ($f);
}
?>
..thus, my filemtime and filectime are always the "fopen" ones.
Still, files created in /tmp and ones created in /home/etc. are not the same. Well, I guess
there's some special mechanism in the kernel for temp files, maybe build for performance
reasons.
----
Manual Page -- http://www.php.net/manual/en/function.filemtime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+40601
Delete -- http://master.php.net/manage/user-notes.php?action=delete+40601&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+40601&report=yes
Search -- http://master.php.net/manage/user-notes.php