PHP clearstatcache() Explained

By Avinash

September 21, 2011Tutorial4 Comments

For the functions like is_file(), file_exists(), etc PHP caches the result of this function for each file for faster performance if function called again.

But in some cases you want to clear this cached information, for the task like getting the information of the same file multiple times in same page.

Points to be note that, PHP also cache the information of the files which not exists. So when you use file_exists() on file which is not exists. PHP will return FALSE untill you create that file.

After creating this file PHP will return TRUE. But when you delete this file manually and call the file_exists() on that file will return TRUE. Because PHP will return the cached informarion.

So always use the unlink() because unlink() will remove the cache of that file automatically.

For clearing the file state cache, clearstatcache() function is used. This function will clear the all cache information stored with below list of 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(), and fileperms().

Here is the example taken from the PHP Manual:

$file = "output_log.txt"; function get_owner($file) { $stat = stat($file); $user = posix_getpwuid($stat["uid"]); return $user["name"]; } $format = "UID @ %s: %s\n"; printf($format, date("r"), get_owner($file)); chown($file, "ross"); printf($format, date("r"), get_owner($file)); clearstatcache(); printf($format, date("r"), get_owner($file)); #OUTPUT UID @ Sun, 12 Oct 2008 20:48:28 +0100: root UID @ Sun, 12 Oct 2008 20:48:28 +0100: root UID @ Sun, 12 Oct 2008 20:48:28 +0100: ross
  $file = "output_log.txt";

  function get_owner($file)
  {
    $stat = stat($file);
    $user = posix_getpwuid($stat["uid"]);
    return $user["name"];
  }

  $format = "UID @ %s: %s\n";

  printf($format, date("r"), get_owner($file));

  chown($file, "ross");
  printf($format, date("r"), get_owner($file));

  clearstatcache();
 
  printf($format, date("r"), get_owner($file));
 
  #OUTPUT
  UID @ Sun, 12 Oct 2008 20:48:28 +0100: root
   UID @ Sun, 12 Oct 2008 20:48:28 +0100: root
   UID @ Sun, 12 Oct 2008 20:48:28 +0100: ross

Note: Always use the unlink(), because unlink() will remove the cache of that file automatically.

Share This Article

  • http://www.boloxe.com/techblog/ Guillaume

    It’s clearstatcache(), not clearstatEcache()

    • http://www.xpertdeveloper.com Avinash

      Hi Guillaume,

      Thanks for noticing. Corrected….

  • http://www.thedigitalorchard.ca/ Ted Wood

    It seems that include() and require() also use the file state cache. On an Nginx server, I’ve run into a problem after renaming the directory where the website lives and then restarting the server. When doing a relative include(), it’s still trying to include from the old directory. Furthermore, calling clearstatcache() seems to have to happen in the same file that’s doing the include() or it won’t have any effect. Very strange and frustrating!

  • Rajasekhar zonup

    I am working with a webcam in my application.
    When i take a snap shot it calls a function to remove the existing image as I used unlink() in an function.
    It gives the old image in my div where i placed a to display the current snap.I checked in firebug and it shows correctly but not displaying the correct in div