For the functions like [code]is_file()[/code], [code]file_exists()[/code], 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 [code]file_exists()[/code] on file which is not exists. PHP will return [code]FALSE[/code] untill you create that file.
After creating this file PHP will return [code]TRUE[/code]. But when you delete this file manually and call the [code]file_exists()[/code] on that file will return [code]TRUE[/code]. Because PHP will return the cached informarion.
So always use the [code]unlink()[/code] 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.
[code]stat()[/code], [code]lstat()[/code], [code]file_exists()[/code], [code]is_writable()[/code], [code]is_readable()[/code], [code]is_executable()[/code], [code]is_file()[/code], [code]is_dir()[/code], [code]is_link()[/code], [code]filectime()[/code], [code]fileatime()[/code], [code]filemtime()[/code], [code]fileinode()[/code], [code]filegroup()[/code], [code]fileowner()[/code], [code]filesize()[/code], [code]filetype()[/code], and [code]fileperms()[/code].
Here is the example taken from the PHP Manual:
[cc lang=”php”]
$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
[/cc]
Note: Always use the [code]unlink()[/code], because [code]unlink()[/code] will remove the cache of that file automatically.
It’s clearstatcache(), not clearstatEcache()
Hi Guillaume,
Thanks for noticing. Corrected….
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!
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