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

?>

このスクリプトを実行する約 1 分前に、テキスト エディターで「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 に答える