Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

filemtime() constant during execution despite changes to file

Tags:

php

filemtime

Try running

<?php
  echo filemtime("test.txt")."\n";
  sleep(4);
  file_put_contents("test.txt", "test");
  echo filemtime("test.txt")."\n";
?>

For me the command line printed:

1343490984
1343490984

That can't be right, can it?

like image 511
Woodgnome Avatar asked Jul 28 '12 16:07

Woodgnome


1 Answers

From the filemtime documentation:

Note: The results of this function are cached. See clearstatcache() for more details.

You need to call clearstatcache() before you call filemtime() again:

echo filemtime("test.txt")."\n";
sleep(4);
file_put_contents("test.txt", "test");
clearstatcache();
echo filemtime("test.txt")."\n";
like image 53
Tim Cooper Avatar answered Sep 27 '22 19:09

Tim Cooper