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 — Limpa as informações em cache sobre arquivos
Descrição
Quando você chama stat(), lstat() ou qualquer uma das funções afetadas (listadas abaixo), o PHP mantém em cache as informações que essas funções retornam para melhoria de performance. Entretanto, em certos casos você pode precisar limpar as informações cacheadas. Por exemplo, se um mesmo arquivo é verificado várias vezes em um único script, e esse arquivo corre o risco de ser apagado ou modificado durante a operação do script, você precisa limpar os dados do cache. Nesses casos, você pode utilizar a função clearstatcache() para limpar todas as informações que o PHP mantém sobre um arquivo.
Você deve notar também que o PHP não guarda informação de cache sobre arquivos
que não existem. Assim, se você chamar file_exists() em um arquivo que
não existe, ele irá retornar FALSE até que você crie o arquivo. Se você criar o arquivo,
ele irá retornar true mesmo que você exclua o arquivo.
Entretanto, unlink() limpa o cache automaticamente.
Nota:
Esta função guarda infomações sobre arquivos específicos, de forma que você somente precisa chamar clearstatcache() se você estiver realizando várias operações sobre o mesmo arquivo e necessita que a informação sobre esse arquivo em particular não seja cacheada.
As funções afetadas são stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype(), e fileperms().
Valor Retornado
Não há valor retornado.
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>> ');
}
?>
