PHP filesize reporting old size

前端 未结 3 716
粉色の甜心
粉色の甜心 2020-11-29 08:58

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 p

相关标签:
3条回答
  • 2020-11-29 09:32

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

    Try calling clearstatcache(); before the filesize call.

    0 讨论(0)
  • 2020-11-29 09:36

    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.

    0 讨论(0)
  • 2020-11-29 09:59

    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);
    
    0 讨论(0)
提交回复
热议问题