PHP clearstatcache()Functions
Complete PHP Filesystem Reference Manual
Definition and Usage
The clearstatcache() function clears the file status cache.
PHP caches the return information of certain functions in order to provide higher performance. However, sometimes, such as when checking the same file multiple times in a script and the file is in danger of being deleted or modified during the execution of the script, you need to clear the file status cache to get the correct result. To do this, use the clearstatcache() function.
Syntax
clearstatcache()
Tips and Notes
Tip:The functions that are cached, i.e., the functions affected by the clearstatcache() function:
- stat()
- lstat()
- file_exists()
- is_writable()
- is_readable()
- is_executable()
- is_file()
- is_dir()
- is_link()
- filectime()
- fileatime()
- filemtime()
- fileinode()
- filegroup()
- fileowner()
- filesize()
- filetype()
- fileperms()
Examples
<?php
//check filesize
echo filesize("test.txt");
echo "<br />";
$file = fopen("test.txt", "a+");
// truncate file
ftruncate($file,100);
fclose($file);
//Clear cache and check filesize again
clearstatcache();
echo filesize("test.txt");
?>
//check filesize
echo filesize("test.txt");
echo "<br />";
$file = fopen("test.txt", "a+");
// truncate file
ftruncate($file,100);
fclose($file);
//Clear cache and check filesize again
clearstatcache();
echo filesize("test.txt");
?>
The above code will output:
792
100
100
Complete PHP Filesystem Reference Manual Other Extensions