Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

caching

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));
like image 501
Rida Fathi Avatar asked Jun 29 '13 13:06

Rida Fathi


1 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.

like image 171
dev-null-dweller Avatar answered Nov 14 '22 23:11

dev-null-dweller