Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Filesystem clearstatcache() Function



The PHP Filesystem clearstatcache() function is used to clear the file status data cache. PHP caches data for particular functions in order to increase efficiency. When a script needs accurate results and accesses a file more than once, clearstatcache() ensures that the cached data is not used.

This feature is useful while working on the same file with multiple actions since it clears cached data, only for those filenames, ensuring that the data is up to date.

Need of clearstatcache() Function

PHP caches the results of various built-in functions, such as stat(), to save resource consumption. Even though this caching improves efficiency, data may become outdated if a file is modified and the function is called later in the script. To ensure valid data after file updates, clearstatcache() needs to be called in order to clear cached information about the file's status.

Syntax

Below is the syntax of the PHP Filesystem clearstatcache() function −

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

Parameters

Here are the required and optional parameters of the clearstatcache() function −

Sr.No Parameter & Description
1

clear_realpath_cache(Required)

A boolean value indicating whether or not to clear the realpath cache. If set to TRUE, it clears the cache.

2

filename(optional)

A string containing the name of the file that have its cache cleared. If a cache is given, only that particular file's cache will be deleted.

Return Value

The function returns NULL.

PHP Version

The clearstatcache() function was first introduced as part of core PHP 4 and work well with the PHP 5, PHP 7, PHP 8.

Example

So we will see the basic usage of the PHP Filesystem clearstatcache() function in this example. So we will just check and print the filesize of the file "sample.txt" which is located in the "/PhpProject" directory.

In the code we have used filesize(), fopen(), ftruncate(), and fclose() functions other than clearstatcache(). These functions are useful to manipulate and get information about the file "/PhpProject/sample.txt", like its size.

<?php
   // check filesize
   echo filesize("/PhpProject/sample.txt");
   echo "\n";

   $file = fopen("/PhpProject/sample.txt", "a+");
   // truncate file
   ftruncate($file, 100);
   fclose($file);

   // Clear cache and check filesize again
   clearstatcache();
   echo filesize("/PhpProject/sample.txt"); 
?>

Output

This will produce the following result −

25
100

Example

We can also use the clearstatcache() method with parameters in PHP to delete specific file information from the cache.

<?php
   $file = "sample.txt";

   // Clears cache for "sample.txt" only
   clearstatcache(true, $file); 
   echo "Cache cleared for $file\n";

   // Clear cache for all files
   clearstatcache(true); 
   echo "Cache cleared for all files\n";

?> 

Output

This will generate the below result −

Cache cleared for all files

Example

Now we will try to clear the cache using clearstatcache() and filesize() function. So we will show the filesize before clearing cache and after clearing cache.

<?php
   // File path
   $file = "/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt";

   // get filesize before clearing cache
   echo "Filesize before clearing cache: " . filesize($file) . "<br>";

   // clear cache for the specific file
   clearstatcache(true, $file);

   // get filesize after clearing cache
   echo "Filesize after clearing cache: " . filesize($file) . "<br>";
?> 

Output

This will lead to the following outcome −

Filesize before clearing cache: 756
Filesize after clearing cache: 305

Affected Functions

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()

Important Notes

  • The unlink() function automatically clears the cache when a file is deleted, then there is no need to use clearstatcache().
  • PHP caches information when using file_exists(), but only for existing files. It does not cache data for files that don't exist.
  • Use clearstatcache() if you are performing multiple operations on the same filename and need to remove cached information about that specific file, as it caches information about specific filenames.

Summary

The function clearstatcache() is very useful for deleting the cached file status and retrieving the most recent information about a file or function. It is an integrated PHP filesystem function.

php_function_reference.htm
Advertisements