【问题标题】:PHP filesize reporting old sizePHP 文件大小报告旧大小
【发布时间】:2011-04-14 11:05:16
【问题描述】:

以下代码是我编写的 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

我做错了什么?

系统是:

  • PHP 5.2.14
  • Apache 2.2.16
  • Linux 内核 2.6.18

【问题讨论】:

  • b 用于输出二进制数据。它不应该导致问题。

标签: php filesize


【解决方案1】:

根据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);

【讨论】:

    【解决方案2】:

    在基于 Linux 的系统上,filesize() 获取的数据是“statcached”。

    尝试在调用文件大小之前调用clearstatcache();

    【讨论】:

      【解决方案3】:

      PHP 将它读取的所有文件元数据存储在缓存中,因此文件大小可能已经存储在该缓存中,您需要清除它。请参阅clearstatcache 并在调用filesize 之前调用它。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多