PHP filesize报告旧的大小

And*_*per 21 php filesize

以下代码是我编写的PHP Web服务的一部分.它需要一些上传的Base64数据,对其进行解码,然后将其附加到文件中.一切正常.

问题是,当我在追加操作后读取文件大小时,我得到文件在追加操作之前的大小.

$fileOut = fopen($filepath.$filename, "ab")
fwrite($fileOut, base64_decode($data));
fflush($fileOut);
fclose($fileOut);

$newSize = filesize($filepath.$filename);   // gives old file size
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

系统是:

  • PHP 5.2.14
  • Apache 2.2.16
  • Linux内核2.6.18

Pek*_*ica 38

在基于Linux的系统上,获取的数据filesize()是"statcached".

尝试clearstatcache();在filesize 调用之前调用.


Chr*_*nte 9

根据PHP手册:

缓存此函数的结果.有关更多详细信息,请参阅clearstatcache().

http://us2.php.net/manual/en/function.filesize.php

基本上,您必须在文件操作后清除stat缓存:

$fileOut = fopen($filepath.$filename, "ab")
fwrite($fileOut, base64_decode($data));
fflush($fileOut);
fclose($fileOut);

clearstatcache();

$newSize = filesize($filepath.$filename);
Run Code Online (Sandbox Code Playgroud)