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 4, PHP 5)
clearstatcache — Czyści bufor statusu pliku
Opis
$clear_realpath_cache = false
[, string $nazwa_pliku
]] )Kiedy używasz stat(), lstat() lub dowolnej z funkcji wymienionej niżej, PHP buforuje informacje, zwrócone przez te funkcje w celu zapewnienia większej wydajności. Jednakże, w kilku przypadkach, możesz chcieć wyczyścić zbuforowane informacje. W przypadku, gdy ten sam plik jest sprawdzany wiele razy w tym samym skrypcie i istnieje obawa, że plik ten zostanie zmieniony lub usunięty podczas pracy skryptu, możesz wywołać czyszczenie bufora stanu. W takim przypadku, możesz użyć funkcji clearstatcache() do wyczyszczenia informacji o pliku zbuforowanej przez PHP.
Weź pod uwagę, że PHP nie buforuje informacji o nie istniejących plikach.
Jeśli wywołasz file_exists() na pliku, który nie istnieje
zostanie zwrócone FALSE dopóki nie stworzysz tego pliku. Jeśli go utworzysz
funkcja zwróci TRUE nawet jeśli go później skasujesz.
Jednakże unlink() czyści automatycznie bufor.
Informacja:
Ta funkcja buforuje informacje o podanych nazwach plików, więc potrzebujesz wywoływać clearstatcache() jeśli wykonujesz wiele operacji na tym samym pliku i potrzebujesz, aby informacje o tym pliku nie były buforowane.
Dotyczy funkcji stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype() i fileperms().
Parametry
-
clear_realpath_cache -
Czy czyścić bufor o prawdziej ścieżce, czy nie.
-
nazwa_pliku -
Czyści bufor o prawdziwej ścieżce dla podanego pliku; tylko jeśli
clear_realpath_cachejestTRUE.
Zwracane wartości
Nie jest zwracana żadna wartość.
Rejestr zmian
| Wersja | Opis |
|---|---|
| 5.3.0 |
Dodano opcjonalne parametry clear_realpath_cache
i filename.
|
Przykłady
Przykład #1 Przykład clearstatcache()
<?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));
?>
Powyższy przykład wyświetli coś podobnego do:
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
On Linux, a forked process inherits a copy of the parent's cache, but after forking the two caches do not impact each other. The snippet below demonstrates this by creating a child and confirming outdated (cached) information, then clearing the cache, and getting new information.
<?php
function report($directory, $prefix = '') { printf('%sDoes %s exist? PHP says "%s"'. PHP_EOL, $prefix, $directory, is_dir($directory) ? 'yes' : 'no'); }
$target = './delete-me-before-running-statcache';
if (is_dir($target)) {
die("Delete $target before running.\n");
}
echo "Creating $target.\n";
mkdir($target) || die("Unable to create $target.\n");
report($target); // is_dir($target) is now cached as true
echo "Unlinking $target.\n";
rmdir($target) || die("Unable to unlink $target.\n");
// This will say "yes", which is old (inaccurate) information.
report($target);
if (($pid = pcntl_fork()) === -1) { die("Failed to pcntl_fork.\n"); }
elseif ($pid === 0) {
// child
report($target, '<<child>> ');
echo "<<child>> Clearing stat cache.\n";
clearstatcache();
report($target, '<<child>> ');
} else {
// parent
sleep(2); // move this to the child block to reverse the test.
report($target, '<<<parent>> ');
clearstatcache();
report($target, '<<<parent>> ');
}
?>
