PHP code to tell me the total disk space used in a directory

Discussion in 'HTML & Website Design' started by Whitey, Feb 23, 2008.

  1. #1
    Hello all

    I need a simple PHP code snippet that will allow me to see the total disk space used by files in my "uploads" directory.

    The script will be placed in the /admin directory and /uploads is in the root folder.

    Thanks:D
     
    Whitey, Feb 23, 2008 IP
  2. rkquest

    rkquest Well-Known Member

    Messages:
    828
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    140
    #2
    You should've posted this in the PHP forums. :D

    Anyway, you'll need recursion for this type of function. I wrote this one and tested it. Works for me.

    
    <?php
    
    function getdirsize($dir) {
    	$size = 0;
    	$files = scandir($dir);
    	foreach ($files as $file) {
    		if (strlen(str_replace('.', '', $file))>0) {
    			if (is_dir($dir.'/'.$file)) {
    				$size += getdirsize($dir.'/'.$file);
    			} else {
    				$size += filesize($dir.'/'.$file);
    			}
    		}
    	}
    	return $size;
    }
    
    echo getdirsize('uploads');
    
    ?>
    
    PHP:
     
    rkquest, Feb 23, 2008 IP
  3. Whitey

    Whitey Active Member

    Messages:
    1,386
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    80
    #3
    Ah yeah.. sorry :D

    Thanks for your help though, ill try it out now :)
     
    Whitey, Feb 24, 2008 IP