Back to Top

What is the use of clearstatcache() in PHP?

What is the use of clearstatcache() in PHP

PHP caches the information when you use the some of the functions php provide to provide the faster performance and improve the execution of the page.Here in this article explains the use of function clearstatcache in PHP

For Example, if the same file is being checked many times using the php function in the script, the files is cached by the PHP so you can choose to use clearstatcache() function to clear the cache state.

What is clearstatcache() function?

The clearstatcache () function clears the file status cache in PHP. The clearstatcache () function will cache certain functions return information in order to provide higher performance. However sometimes, You are using the functions multiple times in the same file or in a script and the file during script execution in danger of being dropped or modified, you need to clear the cache file status, in order to obtain correct results.

Example of clearstatcache() Usage:

Here I am going to explain the runtime example which i recently faced as an issue and clearstatcache() function solved that issue. So Here is the issue. I was using the filesize() function which is used to get the size of the file at the runtime.I was comparing the downloaded filesize with server’s file, okay? So what’s happening. The function filesize() was already used before on that page for logging purpose so it was caching the size of the file and while comparing the file, it was giving me the wrong or old filesize or we can say that if file was exists on that folder so it was giving me the existing filesize rather than new file size after it downloaded from server.

In this case, I have used the clearstatcache () function to clear the information about the PHP cache for the file before calling the filesize() function so it was working like magic.

You should also note that PHP does not cache files that do not exist. Therefore, if you request that the file on file_exists () does not exist, it returns FALSE until you create the file. If you create a file, it returns TRUE even if you then delete the file as it was cached. However, disconnect () function automatically clears the cache.

The following functions are affected of caching and you can use clearstatecache() function before calling below function in your script to clear the cache and get the correct result.

  • 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:

The parameter is empty.

Here is the example of clearstatcache () function example explained using the code:

Example

Hope this post would be helpful and clear your doubt about Clearstatcache function in PHP. Do you find this useful? Let me know in comment section.Don’t forget to share it!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

What Are WordPress Nonces?

Posted on 8 years ago

Bhumi

How to upload image in CodeIgniter?

Posted on 11 years ago

Bhumi