Reputation: 385154
When I run this code:
<?php
$handle = fopen('/tmp/lolwut', 'w') or die("Cannot open File");
fwrite($handle, "1234567890");
fclose($handle);
print_r(filesize('/tmp/lolwut'));
?>
I get the result 10
, which is the correct number of characters in the file.
However, because filesystem blocks are much larger than this, I expected the file size to be "rounded up" to more like 512 bytes or even 1KB. Why is it not?
Upvotes: 4
Views: 231
Reputation: 385154
Do not confuse "file size" for "file size on disk"; PHP's filesize
function gives you the former, not the latter.
Although not explicitly documented as such, filesize
is basically implemented in terms of stat
, and on Linux stat
makes a distinction between filesize and "file size on disk":
All of these system calls return a
stat
structure, which contains the following fields:struct stat { // [...] off_t st_size; /* total size, in bytes */ blksize_t st_blksize; /* blocksize for file system I/O */ blkcnt_t st_blocks; /* number of 512B blocks allocated */ // [...] };
The value you're expecting is st_blocks * st_blksize
, but the "true" filesize st_size
is available regardless.
(This appears to be the case on Windows, too.)
Upvotes: 8