English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and Example of PHP clearstatcache() Function

PHP Filesystem Reference Manual

The clearstatcache() function clears the file status cache. PHP caches data for some functions to improve performance. If a file has been checked several times in the script, we may want to avoid caching to get the correct result, and then use the clearstatcache() function.

Syntax

void clearstatcache ([ bool $clear_realpath_cache = FALSE[, string $filename ]])

The clearstatcache() function caches information about a specific filename, so if we can perform multiple operations on the same filename and do not need to cache information about the specific file, we only need to call the clearstatcache() function.

Online Example

<?php
   //Check file size
   echo filesize("/PhpProject/sample.txt");
   echo "\n";
   $file = fopen("/PhpProject/sample.txt", "a+");
   //Truncate file
   ftruncate($file, 100);
   fclose($file);
   //Clear cache and check file size again
   clearstatcache();
   echo filesize("/PhpProject/sample.txt"); 
?>

Output Result

25
100

PHP Filesystem Reference Manual