Skip to content

Instantly share code, notes, and snippets.

@mattvot
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattvot/877c518f5545b2844433 to your computer and use it in GitHub Desktop.
Save mattvot/877c518f5545b2844433 to your computer and use it in GitHub Desktop.
Filesize method that works with files >2GB on 32bit PHP
<?php
/**
* Filesize method that works with files >2GB on 32bit PHP
*
* @link http://php.net/manual/en/function.filesize.php#113457
* @param string $file Path to file
* @return int|float Size of file
*/
function realFileSize($file)
{
$fp = fopen($file, 'r');
$pos = 0;
$size = 1073741824;
fseek($fp, 0, SEEK_SET);
while ($size > 1)
{
fseek($fp, $size, SEEK_CUR);
if (fgetc($fp) === false)
{
fseek($fp, -$size, SEEK_CUR);
$size = (int)($size / 2);
}
else
{
fseek($fp, -1, SEEK_CUR);
$pos += $size;
}
}
while (fgetc($fp) !== false) $pos++;
fclose($fp);
return $pos;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment