PHP file size does not change after adding

I am having a problem with PHP I / O files.

$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 you can see, I am resizing the file after the initial recording, adding it. Why do I get 9 as the file size in both cases? I expect 18 as output in case 2.

+8
php caching file-io filesize
source share
1 answer

You need to clear the file status cache by calling the clearstatcache function before , you call filesize() again after changing the file:

 // write into file. // call filesize() clearstatcache(); // append to the fiile. // call filesize() 

To improve performance, PHP caches the result of filesize() , so you need to tell PHP to clear this cache before you call filesize() again in the modified file.

+15
source share

All Articles