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!

Share:
Share on Facebook1Share on LinkedIn0Tweet about this on TwitterShare on Google+0Buffer this pageDigg thisShare on Reddit0

Leave a Reply

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

Most Popular Posts

How to convert RGB to Hex color PHP?

Posted on 4 years ago

Bhumi

How to display XML content with PHP

Posted on 4 years ago

Bhumi

How to Setup WordPress Multisite Network?

Posted on 3 years ago

Bhumi

Top 10 JavaScript Charting Libraries

Posted on 2 years ago

Bhumi

Facade implementation in Laravel

Updated 1 month ago

Bhumi

MySQL coalesce() function

Updated 18 years ago

Bhumi

How to use JavaScript Promise API?

Updated 18 years ago

Bhumi

What are Lambdas and Closures in PHP?

Updated 18 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 2 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 3 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 3 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 4 years ago

Bhumi