PHP clearstatcache() Function

What is PHP clearstatcache() Function?

clearstatcache() function clears cached file status and ensures that you receive correct file information.

Why You Need PHP clearstatcache() Function?

PHP caches the results of some PHP built-in functions like stat() returns (see full function list in the list below). This is because these function are expensive in term of resources. By caching a function’s information, PHP saves resource and provides the result from the cache if you call this function again in the script.

But, the downside of this cache is that if modify the file and call the function later in the script, the function still returns the previous information from the cache. So, that means you get the wrong information. So, after any modification of a file, you need to run clearstatcache() function to remove any cached information of this file’s status.

List: PHP cache results of these functions-

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(clear_realpath_cache, filename)

Parameters:

The Function has 2 parameters which are optional-

clear_realpath_cache (Optional): it is a Boolean value. It indicates whether or not clear the realpath cache. The default is FALSE. If you set it as TRUE, the function clears the realpath cache.

Return Values:

The function returns no value.

Examples:

Example 1: Simple clearstatcache() example-

<?php
clearstatcache();
?>

Explanation:

This function clears the entire file status cache.

Example 2: clearstatcache() function with parameters –

<?php
clearstatcache(TRUE, "file/sample.txt");
?>

Explanation:

Here, the clearstatcache() function clears the cached file status of the sample.txt file.

Example 3: Clearing cache for filesize() function-

<?php
$file = 'file/demo.txt';
echo "File size: ". filesize($file)." bytes <br />";
$handle = fopen("file/demo.txt", "a+");
fwrite($handle, 'A fresh content is added.');
fclose($handle);
echo "After added few words, file size becomes: ".filesize($file)." bytes <br />";
clearstatcache();
echo "After clearing cache with clearstatcache(), file size becomes: ".filesize($file)." bytes";
?>

Output:

File size is: 400 bytes
After added few words, file size becomes: 400 bytes
After clearing cache with clearstatcache(), file size becomes: 425 bytes

Explanation:

Line 3: At first, the filesize() function returns the file size as 400 bytes.

Line 5: We added a line of code here. Though, fwrite() function added some text in the file, the filesize() function returns the file size as 400 bytes (line 7). It is because this time the size of the file comes from the cache.

Line 9: After clearing cache with clearstatcache() function (line 8), the filesize() function returns the correct file size as 400 bytes.

Notes on clearstatcache() Function:

  • When you delete a file with PHP unlink() function, it automatically clears the cache also. So, you don’t need to run clearstatcache() separately here.
  • From the function list mention above you see that PHP caches information when you run file_exists() function. It only caches information if the file exists. So, for the non-existence files, PHP doesn’t cache information.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP clearstatcache() Function

clearstatcache() is really a very useful function to clear the cached file status and inform you the most up-to-date information about a file or function. It is a built-in filesystem function in PHP.

Reference:

https://www.php.net/manual/en/function.clearstatcache.php