Display the file size, truncate the file, clear the cache, and then display the file size once more.
| <?php //output 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 result of the code above might be:
| 792 100 |
The clearstatcache() function clears the file status cache.
PHP caches information for certain functions to improve performance. If you need to check a file multiple times within a script and want to avoid cached results to ensure accuracy, you should use the clearstatcache() function.
| clearstatcache(clear_realpath_cache, filename) |
|
Parameter |
Description |
|
clear_realpath_cache |
Optional. Specifies whether to clear the realpath cache. By default, this is set to |
|
filename |
Optional. Specifies a filename and clears the realpath and cache for that particular file only. |
Tip: Functions that use caching include:
|
Return Value: |
Nothing |
|
PHP Version: |
4.0+ |
|
PHP Changelog: |
PHP 5.3 introduced two optional parameters: clear_realpath_cache and filename. |