2011-11-07 35 views
8

我正面临着PHP文件I/O的问题。追加后PHP文件大小不变

$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 !!!!!!! 

可以看到,我在初始写入后通过追加来改变文件大小。为什么我在这两种情况下获得9个文件大小?我期待18输出的情况下,2

+0

该脚本执行后的文件内容是什么? – hsz

+0

亲爱的hsz它有预期的内容。它有18个字符。 – user1033837

回答

15

你需要通过调用函数clearstatcache前清除文件状态缓存在修改文件后再次拨打filesize()

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

clearstatcache(); 

// append to the fiile. 
// call filesize() 

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

+0

真棒!奇迹般有效。 – user1033837