In PHP, clearstatcache() clears the cache that PHP uses to remember file information. Functions such as filesize(), filemtime(), and is_file() save their results so repeated calls are fast.
This cache can cause old values to appear after a file changes in the same script. Call clearstatcache() before you read the file information again to be sure you get the fresh value.
Syntax
Call clearstatcache() with the values you need. Start with this line.
PHP
clearstatcache();
Call
What it does
clearstatcache()
Clears the stat cache for all files.
clearstatcache(true)
Clears the stat cache and the realpath cache as well.
clearstatcache(true, $file)
Clears the cached data for one file only.
PHP does not cache a failed lookup, so file_exists() on a missing file is always fresh. The stat cache is only used for files that exist, and the cache is cleared automatically by functions like unlink(), rename(), and touch().
clearstatcache() makes sure the size is not taken from the cache.
The printed size grows by 3 bytes in each round.
Frequently Asked Questions
It clears the cached file information that PHP keeps for functions like filesize(), filemtime(), and is_file(). After that, those functions read fresh data from the disk.
Call it when a file changes during the same script and you check its size, time, or permissions again. Without it you may see old values.
Not for missing files, because PHP does not cache failed lookups. It can be stale for a file that exists and then is removed by something outside PHP.
Yes. Functions like unlink(), rename(), touch(), chmod(), and chown() clear the cached data for the file they change.
Only a little. Use it only where you really need fresh data, because the cache exists to make repeated checks faster.