Page 1 of 2 12 LastLast
Results 1 to 10 of 18

Thread: PHP: Calculating a Directory File Size

  1. #1
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,261
    Thanks
    3
    Thanked 23 Times in 23 Posts

    Default PHP: Calculating a Directory File Size

    Here are a couple of quick and simple php functions, the first returns the size in bytes contained within the directory that is passed to it, the second function converts the bytes value to a different size unit.
    PHP Code:
    <?php
    function dir_size($dir)
    {
        
    $total_size 0;
        
    $skip = array('.','..','');
        
        
        if( @
    is_resource($handle=opendir($dir)) ) {
            
    $items null;
            while(
    $items[] = readdir($handle)) {
            }
            
    closedir($handle);
            
            foreach (
    $items as $item) {
                if( !
    in_array($item$skip) ) {
                    
    $item_path $dir.'/'.$item;
                    if(
    is_dir($item_path) ) {
                        
    $total_size += dir_size($item_path);
                    } else {
                        
    $total_size += filesize($item_path);
                    }
                    
                }
            }
        }
        return 
    $total_size;
    }

    function 
    convert_bytes_to($value$new_unit='kb')
    {
        switch(
    strtolower($new_unit))
        {
            case 
    'kb':
                
    $value $value/1024;
            break;
            
            case 
    'mb':
                
    $value $value/1048576;
            break;
            
            case 
    'gb':
                
    $value $value/1073741824;
            break;
        }
        return 
    $value;
    }

    $dir 'D:\mydomain.com\wwwroot\mydir';

    $bytes dir_size($dir);

    echo 
    "Total size of $dir = ".$bytes." bytes <br />";
    echo 
    "Total size of $dir = ".convert_bytes_to($bytes,'kb')." kb <br />";
    echo 
    "Total size of $dir = ".convert_bytes_to($bytes,'mb')." mb <br />";
    echo 
    "Total size of $dir = ".convert_bytes_to($bytes,'gb')." gb <br />";
    ?>

  2. #2
    Join Date
    Dec 2005
    Posts
    193
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    And this would include all the subdirs and their contents in the total?
    Schofieldandwhite.com: RFH reseller

  3. #3
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,261
    Thanks
    3
    Thanked 23 Times in 23 Posts

    Default

    Yes, to the best of my knowledge, it includes everything, and that should mean hidden files too.

    I am getting far too carried away with this, the following code snippet does the same as the previous one but for a few things...
    • to reduce rounding errors, the unit conversion now take place on each file, rather than on the total. therefore both functions must be together.
    • I have added a block size variable. As you may well know, on a disk, files take up more space than their actual bytes need because there is a minimum storage unit, or block. So now this is taken into account.
    • The return from the function now is an associative array holding a few different values:
      • sizeondisk - the actual size taken up on the disk, taking into account the blocks.
      • bytesondisk - the actual number of bytes.
      • files - the total number of files.
      • dirs - the total number of folders.

    PHP Code:
    <?php
    function dir_size($dir, &$unit, &$block_size=null)
    {
        
    $skip = array('.','..','');
        
    $returnarr = array();
        
        if( @
    is_resource($handle=opendir($dir)) ) {
            
    $items null;
            while(
    $items[] = readdir($handle)) {
            }
            
    closedir($handle);
            
            foreach (
    $items as $item) {
                if( !
    in_array($item$skip) ) {
                    
    $item_path $dir.'/'.$item;
                    if(
    is_dir($item_path) ) {
                        
    $returnarr['dirs']++;
                        
    $dir_ret dir_size($item_path$unit$block_size);
                        
    $returnarr['dirs'] += $dir_ret['dirs'];
                        
    $returnarr['files'] += $dir_ret['files'];
                        
    $returnarr['bytesondisk'] += $dir_ret['bytesondisk'];
                        
    $returnarr['sizeondisk'] += $dir_ret['sizeondisk'];
                    } else {
                        
    $fsize filesize($item_path);
                        
    $returnarr['files']++;
                        
    $returnarr['bytesondisk'] += $fsize;
                        if(
    $block_size) {
                            
    $blocks round(($fsize/$block_size)+.5,0) ;
                            
    $returnarr['sizeondisk'] += convert_bytes_to(($blocks*$block_size),$unit);
                        } else {
                            
    $returnarr['sizeondisk'] += convert_bytes_to($fsize,$unit);
                        }
                    }
                    
                }
            }
        }
        return 
    $returnarr;
    }
    function 
    convert_bytes_to($value$new_unit)
    {
        switch( 
    strtolower("$new_unit") )
        {
            case 
    'bit':
            case 
    'bits':
                return (
    $value*8);
            break;
            case 
    'kb':
            case 
    'kilobyte':
            case 
    'kilobytes':
                return 
    $value/1024;
            break;
            
            case 
    'mb':
            case 
    'megabyte':
            case 
    'megabytes':
                return 
    $value/1048576;
            break;
            
            case 
    'gb':
            case 
    'gigabyte':
            case 
    'gigabytes':
                return 
    $value/1073741824;
            break;
        }
        return 
    $value;
    }

    $dir 'D:\mydomain.com\wwwroot\mydir';

    $unit 'mb';
    $block_size 4096;
    $total =  dir_size($dir,$unit,$block_size);

    echo 
    "Total size of $dir = ".$total['sizeondisk'].$unit (".$total['bytesondisk']." bytes)<br />";
    echo 
    "The directory is made up of ".$total['files']." files and ".$total['dirs']." folders.<br />";
    ?>

  4. #4
    Join Date
    Dec 2005
    Posts
    193
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks, that's great could you PM me your full name, so I can credit it in my source code (I'm a university student, and although this isn't for uni; I'm in the habit of crediting everything that isn't my work :p )
    Schofieldandwhite.com: RFH reseller

  5. #5
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,261
    Thanks
    3
    Thanked 23 Times in 23 Posts

    Default

    Quote Originally Posted by schofieldandwhite
    Thanks, that's great could you PM me your full name, so I can credit it in my source code (I'm a university student, and although this isn't for uni; I'm in the habit of crediting everything that isn't my work :p )
    Yeah sure, I'll PM you in a few minutes.
    I just had to change it again didn't I! erm, this one counts the hidden files seperately from the rest (though they still all go towards the total size). If I were going to use this myself I'd probably make it into a static class. I'm starting to feel I am posting too many alterations already though, so I won't bother posting any more unless people want me to.
    It's a programmers life eh!
    PHP Code:
    <?php
    function dir_size($dir, &$unit, &$block_size=null)
    {
        
    $skip = array('.','..','');
        
    $returnarr = array();
        
    $returnarr['dirs'] = 0;
        
    $returnarr['files'] = 0;
        
    $returnarr['hiddenfiles'] = 0;
        
    $returnarr['bytesondisk'] = 0;
        
    $returnarr['sizeondisk'] = 0;
        
        if( @
    is_resource($handle=opendir($dir)) ) {
            
    $items null;
            while(
    $items[] = readdir($handle)) {
            }
            
    closedir($handle);
            
            foreach (
    $items as $item) {
                if( !
    in_array($item$skip) ) {
                    
    $item_path $dir.'/'.$item;
                    if(
    is_dir($item_path) ) {
                        
    $returnarr['dirs']++;
                        
    $dir_ret dir_size($item_path$unit$block_size);
                        
    $returnarr['dirs'] += $dir_ret['dirs'];
                        
    $returnarr['files'] += $dir_ret['files'];
                        
    $returnarr['hiddenfiles'] += $dir_ret['hiddenfiles'];
                        
    $returnarr['bytesondisk'] += $dir_ret['bytesondisk'];
                        
    $returnarr['sizeondisk'] += $dir_ret['sizeondisk'];
                    } else {
                        
    $fsize filesize($item_path);
                        
                        if(
    substr($item,0,1) == '.') {
                            
    $returnarr['hiddenfiles']++;
                        } else {
                            
    $returnarr['files']++;
                        }
                        
    $returnarr['bytesondisk'] += $fsize;
                        if(
    $block_size) {
                            
    $blocks round(($fsize/$block_size)+.5,0) ;
                            
    $returnarr['sizeondisk'] += convert_bytes_to(($blocks*$block_size),$unit);
                        } else {
                            
    $returnarr['sizeondisk'] += convert_bytes_to($fsize,$unit);
                        }
                    }
                    
                }
            }
        }
        return 
    $returnarr;
    }
    function 
    convert_bytes_to($value$new_unit)
    {
        switch( 
    strtolower("$new_unit") )
        {
            case 
    'bit':
            case 
    'bits':
                return (
    $value*8);
            break;
            case 
    'kb':
            case 
    'kilobyte':
            case 
    'kilobytes':
                return 
    $value/1024;
            break;
            
            case 
    'mb':
            case 
    'megabyte':
            case 
    'megabytes':
                return 
    $value/1048576;
            break;
            
            case 
    'gb':
            case 
    'gigabyte':
            case 
    'gigabytes':
                return 
    $value/1073741824;
            break;
        }
        return 
    $value;
    }

    $dir 'D:\mydomain.com\wwwroot\mydir';
    $unit 'mb';
    $block_size 4096;
    $total =  dir_size($dir,$unit,$block_size);

    echo 
    "Total size of $dir = ".$total['sizeondisk'].$unit (".$total['bytesondisk']." bytes)<br />";
    echo 
    "The directory is made up of ".($total['files']+$total['hiddenfiles'])." files (".$total['files']." normal and ".$total['hiddenfiles']." hidden) and ".$total['dirs']." folders.<br />";
    ?>

  6. #6
    Join Date
    Dec 2005
    Posts
    193
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Well, I certainly have ended up with a lot more than I expected :p

    Basically, I'm being difficult; rather than using Helm, I'm coding my own control panel. (Helm is good, but it doesn't do quite what I want it to.) Also, this whole project is intended to be a learning experience for me, so, Nick, I will be reading your code very carefully and seeing what I can learn from it. So far, I have gone from knowing nothing of either PHP or SQL to being fairly proficient in PHP and ok in SQL
    Schofieldandwhite.com: RFH reseller

  7. #7
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Yikes - a simple little question (in the other thread) turned into a whole days work for you Nick
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  8. #8
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,261
    Thanks
    3
    Thanked 23 Times in 23 Posts

    Default

    Quote Originally Posted by Warren Ashcroft
    Yikes - a simple little question (in the other thread) turned into a whole days work for you Nick
    Hehe, yeah, it's always more fun working on non-urgent things, have you noticed that
    What is the block size on the RF servers btw, 4k?

  9. #9
    Join Date
    Feb 2004
    Posts
    4,877
    Thanks
    2
    Thanked 134 Times in 113 Posts

    Default

    Quote Originally Posted by nick
    Hehe, yeah, it's always more fun working on non-urgent things, have you noticed that
    What is the block size on the RF servers btw, 4k?
    Block?
    Warren Ashcroft
    Red Fox UK Limited - Pioneers in Internet Technology
    http://www.redfoxuk.com
    w.ashcroft [at] redfoxuk.com

    NOTE: Forum Private Messaging should not be used to contact staff with support queries.

  10. #10
    Join Date
    Mar 2005
    Location
    Isle of Man
    Posts
    1,261
    Thanks
    3
    Thanked 23 Times in 23 Posts

    Default

    Quote Originally Posted by Warren Ashcroft
    Block?
    It's the smallest unit of file storage. On my Mac's HFS+ filesystem I think the block size is 4kb or (4x1024 = 4096 bytes). Every file, no matter how many bytes long must use up a whole number of blocks, so a file of 5000 bytes would actually take up 8192 bytes on the disk. See what I mean?

    I guess your servers are using NTFS which I think is 4kb also, though I'm no expert on these things it must be said.

    what's a block wikipedia?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. New Free Directory
    By fcmisc in forum For Sale, Sites and Services
    Replies: 0
    Last Post: 10th March 2006, 02:07 PM
  2. Popup size
    By CS New Media in forum HTML/CSS/JavaScript
    Replies: 1
    Last Post: 7th January 2006, 05:41 PM
  3. File / Directory access
    By Sol in forum ASP.NET
    Replies: 1
    Last Post: 4th November 2005, 02:07 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •