php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33738 stat() returns wrong size on large files (>2^31 bytes)
Submitted: 2005-07-18 01:10 UTC Modified: 2005-07-18 02:24 UTC
From: mpb dot mail at gmail dot com Assigned:
Status: Not a bug Package: Filesystem function related
PHP Version: 5.0.4 OS: FreeBSD
Private report: No CVE-ID: None
 [2005-07-18 01:10 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-07-18 02:24 UTC] sniper@php.net
RTFM: http://www.php.net/filesize
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 30 02:01:31 2024 UTC