I am facing a problem with PHP file I/O.
$file = fopen("/tmp/test.txt", "w");
fwrite($file,"hi there\n");
fclose($file);
echo filesize("/tmp/test.txt")."\n"; # displays 9
$file = fopen("/tmp/test.txt", "a");
fwrite($file,"hi there\n");
fclose($file);
echo filesize("/tmp/test.txt")."\n"; # also displays 9 !!!!!!!
As one can see, I am changing the file size after the initial write by appending to it. Why do I get 9 as file size in both the cases? I'm expecting 18 as the output in case 2.
You need to clear file status cache by calling the function clearstatcache before you call the filesize()
again after modifying the file:
// write into file.
// call filesize()
clearstatcache();
// append to the fiile.
// call filesize()
In order to get better performance PHP caches the result of filesize()
so you need to tell PHP to clear that cache before you call the filesize()
again on a modified file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With