#33738 [Opn->Bgs]: stat() returns wrong size on large files (>2^31 bytes)
| From: | sniper@php.net | Date: | Mon, 18 Jul 2005 00:24:09 +0000 |
| Subject: | #33738 [Opn->Bgs]: stat() returns wrong size on large files (>2^31 bytes) | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-81986@lists.php.net to get a copy of this message | ||
ID: 33738
Updated by: sniper@php.net
Reported By: mpb dot mail at gmail dot com
-Status: Open
+Status: Bogus
Bug Type: Filesystem function related
Operating System: FreeBSD
PHP Version: 5.0.4
New Comment:
RTFM: http://www.php.net/filesize
Previous Comments:
------------------------------------------------------------------------
[2005-07-18 01:10:10] mpb dot mail at gmail dot com
Description:
------------
stat() returns the wrong size for large files. This is because a 64
bit integer (at the OS level) is truncated to 32 bits (at the PHP
level). (This will not be a problem on platforms where PHP integer
type is 64 bits.)
In C, the stat() function returns st_size as type off_t, which I
suspect is 64 bits on most modern operatings systems.
I'm not sure what the solution for this is. Perhaps if the size is
greater than PHP's maximum interegr value (varies by platform), then
the size is converted to a float? This would be nice.
Looks like the filesize() function has a similar problem.
Perhaps you will consider this to be a "bogus" bug, but I really think
PHP can do better than arbitrarily converting 64 bit ints to 32 bit
ints (which can result in files with negative lengths). If returning a
float is not an option, how about returning a size of -1, or MAX_INT, or
an error code.
Reproduce code:
---------------
// verylargefile is 3751661568 bytes, in this case
$stat = lstat ('/path/to/verylargefile');
$size = $stat['size'];
print "size: $size\n";
Expected result:
----------------
size: 3751661568
Actual result:
--------------
size: -543305728
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=33738&edit=1