[doc-en] master: Example for clearstatcache no longer works. (#6041)
| From: | Tobias Olry via GitHub | Date: | Fri, 06 Nov 2026 12:45:08 +0000 |
| Subject: | [doc-en] master: Example for clearstatcache no longer works. (#6041) | ||
| Groups: | php.doc.cvs | ||
| Request: | Send a blank email to doc-cvs+get-23920@lists.php.net to get a copy of this message | ||
Author: Tobias Olry (tolry)
Committer: GitHub (web-flow)
Pusher: lacatoire
Date: 2026-11-06T13:45:05+01:00
Commit: https://github.com/php/doc-en/commit/124e1f57f5dc2cd667536dc8eb027816effcfead
Raw diff: https://github.com/php/doc-en/commit/124e1f57f5dc2cd667536dc8eb027816effcfead.diff
Example for clearstatcache no longer works. (#6041)
chown has been fixed since https://github.com/php/php-src/commit/930624899b
Modifying the file in another process is the most obvious way to get stale data from the cache.
Changed paths:
M reference/filesystem/functions/clearstatcache.xml
Diff:
diff --git a/reference/filesystem/functions/clearstatcache.xml
b/reference/filesystem/functions/clearstatcache.xml
index de085d247c65..1517d21ad9f8 100644
--- a/reference/filesystem/functions/clearstatcache.xml
+++ b/reference/filesystem/functions/clearstatcache.xml
@@ -108,33 +108,36 @@
<programlisting role="php">
<![CDATA[
<?php
-$file = 'output_log.txt';
-function get_owner($file)
-{
- $stat = stat($file);
- $user = posix_getpwuid($stat['uid']);
- return $user['name'];
-}
+$file = tempnam(sys_get_temp_dir(), 'stat-demo-');
+file_put_contents($file, 'Hello');
-$format = "UID @ %s: %s\n";
+echo "Run this command in another shell:\n";
+echo "\techo \" World!\" >> $file\n";
-printf($format, date('r'), get_owner($file));
+$before = filesize($file); // caches the metadata
-chown($file, 'ross');
-printf($format, date('r'), get_owner($file));
+sleep(20); // Run the echo-command in another shell
+
+$cached = filesize($file); // stale data is returned
clearstatcache();
-printf($format, date('r'), get_owner($file));
-?>
+
+$fresh = filesize($file); // returns the correct data
+
+printf("Before: %d\nCached: %d\nFresh: %d\n", $before, $cached, $fresh);
+
+unlink($file);
]]>
</programlisting>
&example.outputs.similar;
<screen>
<![CDATA[
-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
+Run this command in another shell:
+ echo " World!" >> /tmp/stat-demo-6fjvs4vir9ll2haCl4W
+Before: 5
+Cached: 5
+Fresh: 13
]]>
</screen>
</example>