PHP filesize() On Files > 2 GB

The solution I tried and apparently works is to use the "Size" property of the COM FileObject. I am not entirely sure what type it uses.

This is my code:

function real_filesize($file_path)
{
    $fs = new COM("Scripting.FileSystemObject");
    return $fs->GetFile($file_path)->Size;
}

It's simply called as following:

$file = 'C:\MyFile.rar';
$size = real_filesize($file);
echo "The size of the file is: $size";

Result

The size of the file is: 3,827,394,560 bytes


http://us.php.net/manual/en/function.filesize.php#102135 gives a complete and correct means for finding the size of a file larger than 2GB in PHP, without relying on OS-specific interfaces.

The gist of it is that you first use filesize to get the "low" bits, then open+seek the file to determine how many multiples of 2GB it contains (the "high" bits).


I was using a different approach, saving precious server-resources, have a look at my GitHub repository github.com/eladkarako/download.eladkarako.com.

It is a plain, and complete, download-dashboard, that overcome the (*rare) cases of   filesize   error using client-side head-request, granted, it will not be embedded into the page's HTML source, but rendered (*fixed) some time later, so it is more suitable for hmm..., lets say, relaxed scenarios..

To make this solution available, an Apache .htaccess (or header in PHP) should be added allowing client-side usage of Content-Length value.

Essentially you can slim down the .htaccess to just allowing Content-Length removing other CORS rules.. making the website more secure.

no jQuery was used and whole thing was written in my Samsung text-editor and uploaded by FTP from my smartphone, in a 1.5-hour train-ride in my MILUIM.. and yet, still impeccable ;)