4

我试图在使用 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 秒的时差。如果有人能指出这里发生了什么,那将不胜感激。谢谢。

4

2 回答 2

6

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);
于 2015-11-28T16:27:12.613 回答
1

尝试在 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);
?>
于 2015-11-28T17:44:12.493 回答