Fastest way to calculate the size of an file opened inside the code (PHP)
fstat
determines the file size without any acrobatics:
$f = fopen('file', 'r+');
$stat = fstat($f);
$size = $stat['size'];
ftell
can not be used when the file has been opened with the append("a"
) flag. Also, you have to seek to the end of the file with fseek($f, 0, SEEK_END)
first.
ftell()
can tell you how many bytes are supposed to be in the file, but not how many actually are. Sparse files take up less space on disk than the value seeking to the end and telling will return.