PHP: Hypertext Preprocessor (original) (raw)

clearstatcache

(PHP 4, PHP 5, PHP 7, PHP 8)

clearstatcache — Clears file status cache

Description

clearstatcache(bool $clear_realpath_cache = [false](reserved.constants.php#constant.false), string $filename = ""): void

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](reserved.constants.php#constant.false) until you create the file. If you create the file, it will return [true](reserved.constants.php#constant.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(), andfileperms().

Parameters

clear_realpath_cache

Whether to also clear the realpath cache.

filename

Clear the realpath cache for a specific filename only; only used if clear_realpath_cache is [true](reserved.constants.php#constant.true).

Return Values

No value is returned.

Examples

Example #1 clearstatcache() example

`<?php
$file = 'output_log.txt';

function

get_owner($file)
{ stat=stat(stat = stat(stat=stat(file); user=posixgetpwuid(user = posix_getpwuid(user=posixgetpwuid(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));
?>`

The above example will output something similar to:

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

Found A Problem?

matt_m at me dot com

13 years ago

`unlink() does not clear the cache if you are performing file_exists() on a remote file like:

In this case, even after you unlink() successfully, you must call clearstatcache().

file_exists() then properly returns false.

`

msaladna at apisnetworks dot com

5 years ago

clearstatcache() does not canonicalize the path. clearstatcache(true, "/a/b/c") is different from clearstatcache(true, "/a/b//c").

vechenjivot at gmail dot com

4 years ago

`Not documented, but seems like clearstatcache() is clearing the cache only for the process it is being called from. I have 2 PHP scripts running simultaneously, and the first one does call clearstatcache(), but still the second one deadlocks, unless I call clearstatcache() in it too:

script1:

script2:

I also found this page, which leads to the same conclusion:
https://stackoverflow.com/questions/9251237/clearstatcache-include-path-sessions

`

David Spector

6 years ago

`Note that this function affects only file metadata. However, all the PHP file system functions do their own caching of actual file contents as well. You can use the "realpath_cache_size = 0" directive in PHP.ini to disable the content caching if you like. The default content caching timeout is 120 seconds.

Content caching is not a good idea during development work and for certain kinds of applications, since your code may read in old data from a file whose contents you have just changed.

Note: This is separate from the caching typically done by browsers for all GET requests (the majority of Web accesses) unless the HTTP headers override it. It is also separate from optional Apache server caching.

`

bj at wjblack dot com

10 years ago

`Just to make this more obvious (and so search engines find this easier):

If you do fileops of any kind outside of PHP (say via a system() call), you probably want to clear the stat cache before doing any further tests on the file/dir/whatever. For example:

Pop a clearstatcache() after the system call and all is good (modulo a bit of a performance hit from having a cleared stat cache :-( ).

`

Gabriel

6 years ago

`Definition of $filename parameter let's you think that it expects the filename only but it works if you give the path + filename also.

It should be more clear about this.

`