clearstatcache
(PHP 4, PHP 5, PHP 7, PHP 8)
clearstatcache — Clears file status cache
Description
You should also note that PHP doesn't cache information about non-existent
files. So, if you call is_file() on a file that
doesn't exist, it will return false until you create the file. If you
create the file, it will return true even if you then delete the file.
However unlink() clears the cache automatically.
Note:
This function caches information about specific filenames, so you only
need to call clearstatcache() if you are performing
multiple operations on the same filename and require the information
about that particular file to not be cached.
Affected functions include stat(),
lstat(),
file_exists(),
is_writable(),
is_readable(),
is_executable(),
is_file(),
is_dir(),
is_link(),
filectime(),
fileatime(),
filemtime(),
fileinode(),
filegroup(),
fileowner(),
filesize(),
filetype(), and
fileperms().
file_exists(), is_writable(),
is_readable() and is_executable()
only use the cache for paths that use a wrapper other than the local
filesystem. Local files are checked directly every time.
Parameters
clear_realpath_cache
-
Whether to also clear the realpath cache.
filename
-
Clear the realpath cache for a specific filename only; only
used if clear_realpath_cache is true.
Return Values
No value is returned.
Examples
Example #1 clearstatcache() example
<?php
$file = tempnam(sys_get_temp_dir(), 'stat-demo-');
file_put_contents($file, 'Hello');
echo "Run this command in another shell:\n";
echo "\techo \" World!\" >> $file\n";
$before = filesize($file); // caches the metadata
sleep(20); // Run the echo-command in another shell
$cached = filesize($file); // stale data is returned
clearstatcache();
$fresh = filesize($file); // returns the correct data
printf("Before: %d\nCached: %d\nFresh: %d\n", $before, $cached, $fresh);
unlink($file);
The above example will output
something similar to:
Run this command in another shell:
echo " World!" >> /tmp/stat-demo-6fjvs4vir9ll2haCl4W
Before: 5
Cached: 5
Fresh: 13