I need to get the file size of a file over 2 GB in size. (testing on 4.6 GB file). Is there any way to do this without an external program?
Current status:
filesize()
,stat()
andfseek()
failsfread()
andfeof()
works
There is a possibility to get the file size by reading the file content (extremely slow!).
$size = (float) 0;
$chunksize = 1024 * 1024;
while (!feof($fp)) {
fread($fp, $chunksize);
$size += (float) $chunksize;
}
return $size;
I know how to get it on 64-bit platforms (using fseek($fp, 0, SEEK_END)
and ftell()
), but I need solution for 32-bit platform.
Solution: I've started open-source project for this.
Big File Tools
Big File Tools is a collection of hacks that are needed to manipulate files over 2 GB in PHP (even on 32-bit systems).
One option would be to seek to the 2gb mark and then read the length from there...
So basically that seeks to the largest positive signed integer representable in PHP (2gb for a 32 bit system), and then reads from then on using 8kb blocks (which should be a fair tradeoff for best memory efficiency vs disk transfer efficiency).
Also note that I'm not adding
$chunksize
to size. The reason is thatfread
may actually return more or fewer bytes than$chunksize
depending on a number of possibilities. So instead, usestrlen
to determine the length of the parsed string.Below code works OK for any filesize on any version of PHP / OS / Webserver / Platform.