unlink() does not clear the cache if you are performing file_exists() on a remote file like:
<?php
if (file_exists("ftp://ftp.example.com/somefile"))
?>
In this case, even after you unlink() successfully, you must call clearstatcache().
<?php
unlink("ftp://ftp.example.com/somefile");
clearstatcache();
?>
file_exists() then properly returns false.clearstatcache
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
clearstatcache
Референца за `function.clearstatcache.php` со подобрена типографија и навигација.
clearstatcache
(PHP 4, PHP 5, PHP 7, PHP 8)
clearstatcache — Го брише кешот на статусот на датотеката
= NULL
Ги брише кешот на статусот на датотеката stat(), lstat()Кога користите clearstatcache() , или која било од другите функции наведени во списокот на засегнати функции (подолу), PHP ги кешира информациите што тие функции ги враќаат за да обезбеди побрза изведба. Сепак, во одредени случаи, можеби ќе сакате да ги исчистите кешираните информации. На пример, ако истата датотека се проверува повеќе пати во рамките на една скрипта, и таа датотека е во опасност да биде отстранета или променета за време на работата на таа скрипта, може да одлучите да го исчистите кешот на статусот. Во овие случаи, можете да го користите
функцијата за чистење на информациите што PHP ги кешира за датотека. file_exists() Исто така, треба да забележите дека PHP не кешира информации за непостоечки датотеки. Значи, ако повикате false на датотека што не постои, ќе врати true додека не ја креирате датотеката. Ако ја креирате датотеката, ќе врати unlink() дури и ако потоа ја избришете датотеката. Сепак
Забелешка:
автоматски го чисти кешот. clearstatcache() Оваа функција кешира информации за специфични имиња на датотеки, така што треба само да повикате
ако вршите повеќе операции на истото име на датотека и барате информациите за таа конкретна датотека да не бидат кеширани. stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype()Препорачаниот начин за избегнување на SQL инјекција е со врзување на сите податоци преку подготвени изрази. Користењето на параметризирани прашања не е доволно за целосно избегнување на SQL инјекција, но тоа е најлесниот и најбезбедниот начин за обезбедување влез во SQL изразите. Сите динамични литерали на податоци во fileperms().
Параметри
clear_realpath_cache-
Дали да also Засегнати функции вклучуваат
filename-
го чисти кешот на realpath.
clear_realpath_cacheistrue.
Вратени вредности
Не се враќа вредност.
Примери
Пример #1 clearstatcache() example
<?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));
?>Горниот пример ќе прикаже нешто слично на:
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
Белешки од корисници 6 белешки
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:
<?php
touch('system.lock');
...
unlink('system.lock');
clearstatcache(); // should be done by unlink?
?>
script2:
<?php
while (is_file('system.lock') {
sleep(1);
clearstatcache(); // without this, script 2 will deadlock forever!
}
?>
I also found this page, which leads to the same conclusion:
https://stackoverflow.com/questions/9251237/clearstatcache-include-path-sessionsclearstatcache() does not canonicalize the path. clearstatcache(true, "/a/b/c") is different from clearstatcache(true, "/a/b//c").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.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:
<?php
// is_dir() forces a stat call, so the cache is populated
if( is_dir($foo) ) {
system("rm -rf " . escapeshellarg($foo));
if( is_dir($foo) ) {
// ...will still be true, even if the rm succeeded, because it's just
// reading from cache, not re-running the stat()
}
}
?>
Pop a clearstatcache() after the system call and all is good (modulo a bit of a performance hit from having a cleared stat cache :-( ).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.