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).
if($size>=0x1000000) { $upper+=1; $size-=0x1000000 }
. Your file reading approach is certainly functioning, but not practical. Sadly PHPs fseek(SEEK_CUR) interface does not return the amount skipped, else it would be easier. – Johnnyjohnnycakedisk_free_space()
DOES have skew errors in on large numbers, however, due to its nature, its not possible to be 100% precise anyway. Individual filesystem implementations, cluster sizes, etc, may affect the ACTUAL usable space. So,disk_free_space()
suffers from the inescapable float skew, but it doesn't NEED to be accurate at that level. File sizes are exact numbers, no error tolerance. Screw up the file size, and you will lose data. – Crispy