Home » Php » php – changing the file's last modified time through touch() and getting the result with filemtime()

php – changing the file's last modified time through touch() and getting the result with filemtime()

Posted by: admin July 12, 2020 Leave a comment

Questions:

So I’m trying to get the file’s last modified date and then update it to the current time, but when I look at the result I get the SAME timestamp in both $oldtime and $newtime

$file = 'test.txt';
$oldtime = filemtime($file);
touch($file, time());
$newtime = filemtime($file);

echo '<h1>old</h1>';
print_r(getdate($oldtime));
echo '<h1>new</h1>';
print_r(getdate($newtime));
How to&Answers:

Use clearstatcache after touching file to get proper value of modification time.

Because you have used filemtime before, result for it was cached, and on second call, result is pulled from that cache instead of checking file directly.