filemtime returns same value before and after modification of file

北慕城南 提交于 2019-12-31 00:58:11

问题


Im trying to get the last modified time of a file before and after i write to it using fwrite. But, i get the same values for some reason.

<?php
$i = filemtime('log.txt');
echo gmdate("h:i:s", $i);
echo "<br/>";
$e=fopen('log.txt', 'w');
fwrite($e, "well well well");
$j = filemtime('log.txt');
echo gmdate("h:i:s", $j);

?>

Now i modify 'log.txt' with a text editor about a minute before i run this script. So i should be getting about 40-60 seconds of time difference. If someone could point out what is happening here, that'd be really appreciated. Thanks.


回答1:


The documentation of filemtime states that results of this function are cached. Maybe you can try it with clearstatcache:

<?php
$i = filemtime('log.txt');
echo gmdate("h:i:s", $i);
echo "<br/>";
$e=fopen('log.txt', 'w');
fwrite($e, "well well well");
clearstatcache();
$j = filemtime('log.txt');
echo gmdate("h:i:s", $j);



回答2:


Try to add fclose after the fwrite:

<?php
$i = filemtime('log.txt');
echo gmdate("h:i:s", $i);
echo "<br/>";
$e=fopen('log.txt', 'w');
fwrite($e, "well well well");
fclose($e);
$j = filemtime('log.txt');
echo gmdate("h:i:s", $j);
?>


来源:https://stackoverflow.com/questions/33973023/filemtime-returns-same-value-before-and-after-modification-of-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!