PHP clearstatcache() Function
Definition and Usage
The clearstatcache() function clears the file status cache.
The clearstatcache() function caches the return information of some functions to provide higher performance. However, sometimes, when you need to clear the file status cache to obtain the correct results, for example, when checking the same file multiple times in a script, and there is a risk that the file may be deleted or modified during the execution of the script, you need to use the clearstatcache() function to do so.
Functions that will be cached, that is, 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()
Syntax
clearstatcache()
Example
<?php // Check the file size echo filesize("test.txt"); $file = fopen("test.txt", "a+"); // Truncate the file ftruncate($file,100); fclose($file); // Clear the cache and check the file size again clearstatcache(); echo filesize("test.txt"); ?>
Output:
792 100