PHP filesize reporting old size

杀马特。学长 韩版系。学妹 提交于 2019-11-26 14:23:17

问题


The following code is part of a PHP web-service I've written. It takes some uploaded Base64 data, decodes it, and appends it to a file. This all works fine.

The problem is that when I read the file size after the append operation I get the size the file was before the append operation.

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

$newSize = filesize($filepath.$filename);   // gives old file size

What am I doing wrong?

System is:

  • PHP 5.2.14
  • Apache 2.2.16
  • Linux kernel 2.6.18

回答1:


On Linux based systems, data fetched by filesize() is "statcached".

Try calling clearstatcache(); before the filesize call.




回答2:


According to the PHP manual:

The results of this function are cached. See clearstatcache() for more details.

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

Basically, you have to clear the stat cache after the file operation:

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

clearstatcache();

$newSize = filesize($filepath.$filename);



回答3:


PHP stores all file metadata it reads in a cache, so it's likely that the file size is already stored in that cache, and you need to clear it. See clearstatcache and call it before you call filesize.



来源:https://stackoverflow.com/questions/3747982/php-filesize-reporting-old-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!