# Calculate folder size in computer

**URL:** <https://www.phphelp.com/t/calculate-folder-size-in-computer/34152>\
**Category:** General Chat\
**Created:** [October 23, 2021, 2:15pm UTC](https://www.phphelp.com/t/calculate-folder-size-in-computer/34152 "2021-10-23T14:15:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mohammad](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@Mohammad](https://www.phphelp.com/u/Mohammad)\
**Post date:** [October 23, 2021, 2:15pm UTC](https://www.phphelp.com/t/calculate-folder-size-in-computer/34152/1 "2021-10-23T14:15:03Z")

</div>

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 ):

 ![empty folders and files size](https://www.phphelp.com/uploads/default/original/2X/1/12f09a9102c4638aa52cb970569761415dd86d57.jpeg)  
Now i want to know what is this folder size, and one folder with 4KB size can has how much files ?

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [October 23, 2021, 3:08pm UTC](https://www.phphelp.com/t/calculate-folder-size-in-computer/34152/2 "2021-10-23T15:08:01Z")

</div>

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 ) 🙂

```auto
<?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:

```auto
<?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…

---

<div class="post-metadata">

**Author:** ![Mohammad](https://www.phphelp.com/letter_avatar_proxy/v4/letter/m/e274bd/32.png) [@Mohammad](https://www.phphelp.com/u/Mohammad)\
**Post date:** [October 24, 2021, 2:26pm UTC](https://www.phphelp.com/t/calculate-folder-size-in-computer/34152/3 "2021-10-24T14:26:59Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ErnieAlex](https://www.phphelp.com/letter_avatar_proxy/v4/letter/e/bc8723/32.png) [@ErnieAlex](https://www.phphelp.com/u/ErnieAlex)\
**Post date:** [October 24, 2021, 8:51pm UTC](https://www.phphelp.com/t/calculate-folder-size-in-computer/34152/4 "2021-10-24T20:51:38Z")

</div>

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