PHP Reads Folder Size (PRFS)


PRFS:   SOLD       




Once, I was building a website focused on file management, where it needed to read the size of folders and subfolders. Here's a legacy function that accomplishes that:

<?php

function folderSize($dir)
{
    $size = 0;

    foreach (glob(rtrim($dir, '/') . '/*', GLOB_NOSORT) as $each) {
        $size += is_file($each) ? filesize($each) : folderSize($each);
    }

    return $size;
}


At least on Linux, this won't include dot files (since * doesn't match .some-file). Additionally, on my machine, empty directories still occupy some space (4096 bytes according to du), which is also not included in folderSize. Oh, and thanks—I’m using something quite similar.


Post a Comment

Previous Next

نموذج الاتصال