title: PHP filesize over 4Gb
body: <p>I'm running a Synology NAS Server,
  and I'm trying to use PHP to get the filesize of files.<br />
  I'm trying to find a function that will successfully calculate the filesize of files over 4Gb.</p>
  
  <p><code>filesize($file);</code> only works for files &lt;2Gb<br />
  <code>sprintf("%u", filesize($file));</code> only works for files &lt;4Gb<br /></p>
  
  <p>I also tried another function that I found on the php manual, but it doesn't work properly.<br />
  It randomly works for certain file sizes but not for others.</p>
  
  <pre><code>function fsize($file) {
    // filesize will only return the lower 32 bits of
    // the file's size! Make it unsigned.
    $fmod = filesize($file);
    if ($fmod &lt; 0) $fmod += 2.0 * (PHP_INT_MAX + 1);
  
    // find the upper 32 bits
    $i = 0;
  
    $myfile = fopen($file, "r");
  
    // feof has undefined behaviour for big files.
    // after we hit the eof with fseek,
    // fread may not be able to detect the eof,
    // but it also can't read bytes, so use it as an
    // indicator.
    while (strlen(fread($myfile, 1)) === 1) {
      fseek($myfile, PHP_INT_MAX, SEEK_CUR);
      $i++;
    }
  
    fclose($myfile);
  
    // $i is a multiplier for PHP_INT_MAX byte blocks.
    // return to the last multiple of 4, as filesize has modulo of 4 GB (lower 32 bits)
    if ($i % 2 == 1) $i--;
  
    // add the lower 32 bit to our PHP_INT_MAX multiplier
    return ((float)($i) * (PHP_INT_MAX + 1)) + $fmod;
  }
  </code></pre>
  
  <p>Any ideas?</p>