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.
Description
$clear_realpath_cache = false
[, string $filename
]] )L'appel à la fonction stat() ou lstat() est relativement coûteux en termes de temps d'exécution. Pour cela, le résultat du dernier appel à l'une des fonctions de statut, (voir la liste ci-dessous), est sauvegardé pour réutilisation ultérieure. Si vous voulez forcer la vérification du statut d'un fichier, dans le cas où le fichier aurait pu être modifié ou aurait disparu, vous devez utiliser la fonction clearstatcache() afin d'effacer de la mémoire les résultats du dernier appel à la fonction.
Sachez que PHP ne met pas en cache les informations concernant
un fichier inexistant. Si vous appelez file_exists()
sur un fichier qui n'existe pas, la fonction retournera FALSE
jusqu'à ce que vous créiez le fichier. Si vous créez le fichier,
la fonction retournera TRUE même si vous effacez le fichier.
Note:
Cette fonction met en cache des informations sur les fichiers. Vous n'avez donc besoin d'appeler clearstatcache() que si vous faites des opérations multiples sur le dossier, et que vous voulez avoir une version récente des informations.
Les fonctions affectées sont : stat(), lstat(), file_exists(), is_writable(), is_readable(), is_executable(), is_file(), is_dir(), is_link(), filectime(), fileatime(), filemtime(), fileinode(), filegroup(), fileowner(), filesize(), filetype(), et fileperms().
Liste de paramètres
-
clear_realpath_cache -
Si l'on doit ou non vider le cache réel.
-
filename -
Nettoie le cache réel ainsi que le cache stat d'un fichier spécifique. Ne peut être utilisé que si le paramètre
clear_realpath_cachevautTRUE.
Valeurs de retour
Aucune valeur n'est retournée.
Historique
| Version | Description |
|---|---|
| 5.3.0 |
Ajout des paramètres optionnels clear_realpath_cache
et filename.
|
Exemples
Exemple #1 Exemple avec 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));
?>
L'exemple ci-dessus va afficher quelque chose de similaire à :
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>> ');
}
?>
