附加后PHP文件大小不变

use*_*837 8 php file-io caching filesize

我正面临着PHP文件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 !!!!!!!
Run Code Online (Sandbox Code Playgroud)

可以看到,我在初始写入后通过附加更改文件大小.在这两种情况下,为什么我的文件大小为9?我期待在案例2中输出18作为输出.

cod*_*ict 15

在修改文件后再次调用之前,需要通过调用函数clearstatcache 来清除文件状态缓存:filesize()

// write into file.
// call filesize()

clearstatcache();

// append to the fiile.
// call filesize()
Run Code Online (Sandbox Code Playgroud)

为了获得更好的性能,PHP缓存了结果,filesize()因此您需要告诉PHP在filesize()再次调用修改后的文件之前清除该缓存.