
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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.