How does PHP fstat() function work?
Does the function read file size from disk on every call?
Or does the function calculate the size based on all writing operations performed?
Example:
$filename='abc.txt';
$fp=fopen($filename, 'a');
$fstat=fstat($fp);
echo 'Size: '.$fstat['size'].'<br><br>';
echo 'Writing...<br><br>';
fwrite($fp, 'xx');
fwrite($fp, 'yyyy');
// ...
// Some number of fwrite() opertions
// ...
fwrite($fp, 'zzzzzz');
$fstat=fstat($fp);
echo 'Size after writing: '.$fstat['size'].'<br>';
// Does the size is read from disk or is calculated based on earlier writing operations?
fclose($fp);
I suspect you are asking because the size is not as you expect. And I suspect it is not as you expect because you read the size before closing the file when some writes are still buffered.
Try closing the file first and then using
stat():