PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

copy> <chown
Last updated: Fri, 10 Oct 2008

view this page in

clearstatcache

(PHP 4, PHP 5)

clearstatcacheClears file status cache

Description

void clearstatcache ( void )

When you use stat(), lstat(), or any of the other functions listed in the affected functions list (below), PHP caches the information those functions return in order to provide faster performance. However, in certain cases, you may want to clear the cached information. For instance, if the same file is being checked multiple times within a single script, and that file is in danger of being removed or changed during that script's operation, you may elect to clear the status cache. In these cases, you can use the clearstatcache() function to clear the information that PHP caches about a file.

You should also note that PHP doesn't cache information about non-existent files. So, if you call file_exists() on a file that doesn't exist, it will return FALSE until you create the file. If you create the file, it will return TRUE even if you then delete the file. However unlink() clears the cache automatically.

Note: This function caches information about specific filenames, so you only need to call clearstatcache() if you are performing multiple operations on the same filename and require the information about that particular file to not be cached.

Affected functions include 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().

Return Values

No value is returned.



copy> <chown
Last updated: Fri, 10 Oct 2008
 
add a note add a note User Contributed Notes
clearstatcache
Anonymous
26-Sep-2008 05:49
re stangelanda at gmail dot com
I have tested this on Windows XP SP3, PHP Version 5.2.5 and I got a warning:

filesize() stat failed for $filename on the second call to the filesize() function

ie it worked as I would expect so unless the bug has been introduced since then - no bug.
BlueCamel
06-Aug-2008 06:58
Regarding the previous user note, the docs say that calling unlink() for a file will clear any stats cache for that file. If you're seeing the described windows behavior it sounds like a possible PHP bug or doc bug.
stangelanda at gmail dot com
24-Jan-2008 12:35
It should be noted that a call to any of those functions will cache all of the file's information, not just the information that is returned.

Obviously it is clear that the following requires you to clear the cache.
<?php
    $size1
= filesize($filename);
   
unlink($filename);   
   
$size2 = filesize($filename);
   
// $size2 still equals $size1, unless you cleared the stat cache in between.
?>

<?php
    $access
= fileatime($filename);
   
unlink($filename);   
   
$size = filesize($filename);
   
// $size will be the filesize before it was unlinked, because the filesize was cached when fileatime was called, even though the two functions wouldn't appear to have anything to do with either other.
?>

Confirmed on a windows system.

copy> <chown
Last updated: Fri, 10 Oct 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites