|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2004-06-15 15:43 UTC] jnoll at prim dot hu
Description:
------------
The problem is the following:
1. open a file for writing or appending
2. append a character (or anything) to that file
3. close file
4. get the file size with stat or filesize
ERROR: this will return the OLD size, before writing
If you do a clearstatcache(), the filesize will be the NEW size (the expected result)
Reproduce code:
---------------
<?
$filename = "/tmp/stattest";
// initialise
if (!file_exists($filename)) {
$f = fopen($filename,"w");
fclose($f);
echo "Reload, please.";
exit;
}
$fs = filesize($filename); $fstat = stat($filename);
echo "START: $fs, ".$fstat['size']."<BR>";
$f = fopen($filename,"a");
fputs($f,"x");
fclose($f);
/**
* bad result: this should be 1 byte more than the previous
* BUT IT IS THE SAME!!!
*/
$fs = filesize($filename); $fstat = stat($filename);
echo "AFTER(BAD): $fs, ".$fstat['size']."<BR>";
// force a clear
clearstatcache();
/**
* good result after clearstatcache (why???)
*/
$fs = filesize($filename); $fstat = stat($filename);
echo "AFTER(GOOD): $fs, ".$fstat['size']."<BR>";
?>
Expected result:
----------------
START: 145, 145
AFTER(BAD): 145, 145
AFTER(GOOD): 146, 146
If AFTER(BAD) is less than AFTER(GOOD) value, that means PHP is buggy.
PatchesPull Requests
Pull requests:
HistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Wed Oct 29 15:00:02 2025 UTC |
When writing a daemon process that accesses files very often it seems litterally stupid to cache stat on these files. With Basic Code such as the below, the statcache adds unneccessary overhead and actully SLOWS down the code caching & purging the cache on EACH and EVERY revolution of code. There MUST be an option to disable it, other than rm -rf'ing the stuff from the src/. Regardless if you believe its "quicker" or not is moot, for each and every case that you give where it is quicker, I can garuntee to give you a seperate case proving where it slows the code down. If your not willing to add a config option, how about a runtime option? (after all, who needs ob_implicit_flush()? its just another 'useless' function..., same idioligy applies here). And there are PLUNTY of cases where code such as this is required (obviously, not the exact code, its pseudo code) while (TRUE) { clearstatcache(); if (file_exists("file.tmp")) { process_stuff; } }