Calculate folder size in computer

hello i have a question about folders size: how can i know what is the size of a folder ? i use this code for create a lot empty files and folders in a folder:

<?php while ( 1 == 1 ) { touch(rand()); mkdir(rand()); } ?>

see this photo ( folder t after almost 20 minutes execute code ):


Now i want to know what is this folder size, and one folder with 4KB size can has how much files ?

Well, a folder takes 4k in size by default. This is the minimum size of an entry in the VOC.
( Volume Of Contents ) It is without anything inside it. Once you add files to that folder, it will have a new size based on the items inside it.

Not sure what you are asking. Do you want PHP code that can calculate size of a folder?
If it is a Windows server, you can use this routine ( From StackOverflow ) :slight_smile:

<?PHP
    $f = 'f:/www/docs';
    $obj = new COM ( 'scripting.filesystemobject' );
    if ( is_object ( $obj ) )
    {
        $ref = $obj->getfolder ( $f );
        echo 'Directory: ' . $f . ' => Size: ' . $ref->size;
        $obj = null;
    }
    else
    {
        echo 'can not create object';
    }
?>

But, if it is a Linux hosting server, you can use this:

<?PHP
    $f = './path/directory';
    $io = popen ( '/usr/bin/du -sk ' . $f, 'r' );
    $size = fgets ( $io, 4096);
    $size = substr ( $size, 0, strpos ( $size, "\t" ) );
    pclose ( $io );
    echo 'Directory: ' . $f . ' => Size: ' . $size;
?>

You would need to enter the folder you are checking on of course with the correct path to it.
Now, you could use a recursive folder like the PHP RecursiveDirectoryIterator() function or one of the others available to check the size of all folders in a root folder. Hope this helps…

I deleted that folder and i dont have access to it now, i want to calculate manually folder size

Manually calculate folder size? Just right-click on it and look at properties. It will tell you the size.

Sponsor our Newsletter | Privacy Policy | Terms of Service