filemtime在修改文件前后返回相同的值


filemtime returns same value before and after modification of file

我试图在使用fwrite写入文件之前和之后获取文件的最后修改时间。但是,出于某种原因,我得到了相同的值。

<?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);
?>

现在,在运行这个脚本之前大约一分钟,我用文本编辑器修改了"log.txt"。所以我应该有40-60秒的时差。如果有人能指出这里正在发生的事情,我们将不胜感激。谢谢

filemtime的文档说明缓存了此函数的结果。也许你可以试试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);

尝试在fwrite:之后添加fclose

<?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);
?>