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:
Note: Always use the unlink(), because unlink() will remove the cache of that file automatically.
Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.
View all posts by: Avinash