php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54758 GetSize() returns negative values for large file sizes
Submitted: 2011-05-16 21:46 UTC Modified: 2015-07-22 20:23 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: noodleyman at gmail dot com Assigned: cmb (profile)
Status: Duplicate Package: Directory function related
PHP Version: 5.3.6 OS: Windows Server 2008
Private report: No CVE-ID: None
 [2011-05-16 21:46 UTC] noodleyman at gmail dot com
Description:
------------
When using GetFile() as documented http://php.net/manual/en/directoryiterator.getsize.php if you run the function on a large file you get negative values which are wrong. I haven't got the exact file size that this happens at, but I have tested with files of 7GB and over, and always get the same result. tested using the function in the test script.

Test script:
---------------
function dirSize($directory) {
    $size = 0;
    foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file){
        $size+=$file->getSize();
    }
    return $size;
} 

Then ran function using input value of a directory with a single file of 8,510,398,464 bytes in size.

Expected result:
----------------
8310936 KB

Actual result:
--------------
-77674.63

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-05-16 22:02 UTC] noodleyman at gmail dot com
just realised I typed the wrong function name in the description by mistake. it should read "When using GetSize()" not "When using GetFile()"
 [2011-05-17 10:11 UTC] noodleyman at gmail dot com
Just confirming that I have also tested this using the code in the example page as linked to below and here are the results


<?php
$iterator = new DirectoryIterator(dirname(__FILE__));
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isFile()) {
        echo $fileinfo->getFilename() . " " . $fileinfo->getSize() . "\n";
    }
}
?>

on the same file, this returns a result of "-79538816"
 [2011-05-17 10:14 UTC] noodleyman at gmail dot com
the filesize() function also returns the same results as GetSize() for large files.
 [2011-05-17 11:21 UTC] noodleyman at gmail dot com
now using these functions to get a work around. 

function dos_filesize($fn) {
    if (is_file($fn))
        return exec('FOR %A IN ("'.$fn.'") DO @ECHO %~zA');
    else
        return '0';
}

function dirSize($directory) {
    $size = 0;
    foreach(new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file){
        $size+=dos_filesize($file);
    }
    return $size;
}
 [2011-10-14 15:36 UTC] clicky at erebot dot net
This is a duplicate of bug #27792.
Short answer is: PHP does not support large files (>= 2GB) as of yet.
 [2015-07-22 20:23 UTC] cmb@php.net
-Status: Open +Status: Duplicate -Assigned To: +Assigned To: cmb
 [2015-07-22 20:23 UTC] cmb@php.net
Indeed, clicky!
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Dec 04 08:01:29 2024 UTC