<p>I'm looking for something that will show me the size of each folder within my main folder recursively.</p>

<p>This is a <a href="http://en.wikipedia.org/wiki/LAMP_%28software_bundle%29" rel="nofollow noreferrer">LAMP</a> server with a CGI-Bin so most any PHP script should work or anything that will work in the CGI-Bin.</p>

<p>My hosting company does not provide an interface for me to see which folders are consuming the most amount of space.  I don't know of anything on the Internet and did a few searches however I came up with no results.  </p>

<p>Something implementing graphs (<a href="http://en.wikipedia.org/wiki/GD_Graphics_Library" rel="nofollow noreferrer">GD</a>/<a href="http://en.wikipedia.org/wiki/ImageMagick" rel="nofollow noreferrer">ImageMagick</a>) would be best but not required.</p>

<p>My host supports only Perl in the CGI-BIN.</p>

[{'answer_id': 29755, 'author': 'lubos hasko', 'author_id': 275, 'author_profile': 'https://Stackoverflow.com/users/275', 'pm_score': 4, 'selected': True, 'text': '<p>Strange, I came up on Google with <a href="http://www.google.com.au/search?hl=en&amp;q=php+directory+size&amp;btnG=Google+Search&amp;meta=" rel="nofollow noreferrer">many relevant results</a> and <a href="http://www.go4expert.com/forums/showthread.php?t=290" rel="nofollow noreferrer">this one</a> is probably the most complete.</p>\n\n<blockquote>\n  <p>The function "getDirectorySize" will\n  ignore link/shorcuts to\n  files/directory. The function\n  "sizeFormat" will suffix the size with\n  bytes,KB,MB or GB accordingly.</p>\n</blockquote>\n\n<h2>Code</h2>\n\n<pre><code>function getDirectorySize($path)\n{\n  $totalsize = 0;\n  $totalcount = 0;\n  $dircount = 0;\n  if ($handle = opendir ($path))\n  {\n    while (false !== ($file = readdir($handle)))\n    {\n      $nextpath = $path . \'/\' . $file;\n      if ($file != \'.\' &amp;&amp; $file != \'..\' &amp;&amp; !is_link ($nextpath))\n      {\n        if (is_dir ($nextpath))\n        {\n          $dircount++;\n          $result = getDirectorySize($nextpath);\n          $totalsize += $result[\'size\'];\n          $totalcount += $result[\'count\'];\n          $dircount += $result[\'dircount\'];\n        }\n        elseif (is_file ($nextpath))\n        {\n          $totalsize += filesize ($nextpath);\n          $totalcount++;\n        }\n      }\n    }\n  }\n  closedir ($handle);\n  $total[\'size\'] = $totalsize;\n  $total[\'count\'] = $totalcount;\n  $total[\'dircount\'] = $dircount;\n  return $total;\n}\n\nfunction sizeFormat($size)\n{\n    if($size&lt;1024)\n    {\n        return $size." bytes";\n    }\n    else if($size&lt;(1024*1024))\n    {\n        $size=round($size/1024,1);\n        return $size." KB";\n    }\n    else if($size&lt;(1024*1024*1024))\n    {\n        $size=round($size/(1024*1024),1);\n        return $size." MB";\n    }\n    else\n    {\n        $size=round($size/(1024*1024*1024),1);\n        return $size." GB";\n    }\n\n}\n</code></pre>\n\n<h2>Usage</h2>\n\n<pre><code>$path="/httpd/html/pradeep/";\n$ar=getDirectorySize($path);\n\necho "&lt;h4&gt;Details for the path : $path&lt;/h4&gt;";\necho "Total size : ".sizeFormat($ar[\'size\'])."&lt;br&gt;";\necho "No. of files : ".$ar[\'count\']."&lt;br&gt;";\necho "No. of directories : ".$ar[\'dircount\']."&lt;br&gt;"; \n</code></pre>\n\n<h2>Output</h2>\n\n<pre><code>Details for the path : /httpd/html/pradeep/\nTotal size : 2.9 MB\nNo. of files : 196\nNo. of directories : 20\n</code></pre>\n'}, {'answer_id': 31911, 'author': 'John Douthat', 'author_id': 2774, 'author_profile': 'https://Stackoverflow.com/users/2774', 'pm_score': 1, 'selected': False, 'text': '<p>If you have shell access you can run the command </p>\n\n<pre><code>$ du -h\n</code></pre>\n\n<p>or perhaps use this, if PHP is configured to allow execution:</p>\n\n<pre><code>&lt;?php $d = escapeshellcmd(dirname(__FILE__)); echo nl2br(`du -h $d`) ?&gt;\n</code></pre>\n'}, {'answer_id': 39269519, 'author': 'nilopa', 'author_id': 6782895, 'author_profile': 'https://Stackoverflow.com/users/6782895', 'pm_score': 0, 'selected': False, 'text': '<h1>number_files_and_size.php</h1>\n\n<pre><code>&lt;?php\nif (isset($_POST["nivel"])) {\n        $mostrar_hasta_nivel = $_POST["nivel"];\n        $comenzar_nivel_inferior = $_POST["comenzar_nivel_inferior"];\n        // $mostrar_hasta_nivel = 3;\n\n        global $nivel_directorio_raiz;\n        global $nivel_directorio;\n\n        $path = dirname(__FILE__);\n        if ($comenzar_nivel_inferior == "si") {\n            $path = substr($path, 0, strrpos($path, "/"));\n        }\n        $nivel_directorio_raiz = count(explode("/", $path)) - 1;\n        $numero_fila = 1;\n\n\n        // Comienzo de Tabla\n        echo "&lt;table border=\'1\' cellpadding=\'3\' cellspacing=\'0\'&gt;";\n        // Fila encabezado\n        echo "&lt;tr style=\'font-size: 100%; font-weight: bold;\' bgcolor=\'#e2e2e2\'&gt;&lt;td&gt;&lt;/td&gt;&lt;td&gt;Ruta&lt;/td&gt;&lt;td align=\'center\'&gt;Nivel&lt;/td&gt;&lt;td align=\'right\' style=\'color:#0000ff;\'&gt;Ficheros&lt;/td&gt;&lt;td align=\'right\'&gt;Acumulado fich.&lt;/td&gt;&lt;td align=\'right\'&gt;Directorio&lt;/td&gt;&lt;td align=\'right\' style=\'color:#0000ff;\'&gt;Tama&amp;ntilde;o&lt;/td&gt;&lt;td align=\'right\'&gt;Acumulado tama&amp;ntilde;o&lt;/td&gt;&lt;/tr&gt;";\n        // Inicio Filas de datos\n        echo "&lt;tr&gt;";\n\n        //Función que se invoca a si misma de forma recursiva según recorre el directorio raiz ($path)\n        FileCount($path, $mostrar_hasta_nivel, $nivel_directorio_raiz); \n\n        // Din Filas de datos\n        echo "&lt;/tr&gt;";\n        // Fin de tabla\n        echo "&lt;/table&gt;";\n        echo "&lt;div style=\'font-size: 120%;\'&gt;";\n        echo "&lt;br&gt;Total ficheros en la ruta &lt;b&gt;&lt;em&gt;" . $path . ":&lt;/em&gt; " . number_format($count,0,",",".") . "&lt;/b&gt;&lt;br&gt;";\n        echo "Tama&amp;ntilde;o total ficheros: &lt;b&gt;". number_format($acumulado_tamanho, 0,",",".") . " Kb.&lt;/b&gt;&lt;br&gt;";\n        echo "&lt;/div&gt;";\n\n        echo "&lt;div style=\'min-height: 60px;\'&gt;&lt;/div&gt;";\n\n} else {\n    ?&gt;\n    &lt;form name="formulario" id="formulario" method="post" action="&lt;?php echo $_SERVER[\'PHP_SELF\']; ?&gt;"&gt;\n        &lt;br /&gt;&lt;h2&gt;Informe del Alojamiento por directorios (N&amp;uacute;mero de Archivos y Tama&amp;ntilde;o)&lt;/h2&gt;\n        &lt;br /&gt;Nivel de directorios a mostrar: &lt;input type="text" name="nivel" id="nivel" value="3"&gt;&lt;br /&gt;&lt;br /&gt;\n        &lt;input type="checkbox" name="comenzar_nivel_inferior" value="si" checked="checked"/&gt; Comenzar en nivel de directorio inmediatamente inferior a la ubicaci&amp;oacute;n de este m&amp;oacute;dulo PHP&lt;br /&gt;(&lt;?php echo dirname(__FILE__) ?&gt;)&lt;br /&gt;&lt;br /&gt;\n        &lt;input type="submit" name="comenzar" id="comenzar" value="Comenzar proceso"&gt;&lt;br /&gt;&lt;br /&gt;\n    &lt;/form&gt;\n    &lt;?php\n}\n\n\n\n\nfunction FileCount($dir, $mostrar_hasta_nivel, $nivel_directorio_raiz){\n    global $count;\n    global $count_anterior;\n    global $suma_tamanho;\n    global $acumulado_tamanho;\n\n    $arr=explode(\'&amp;\',$dir);\n    foreach($arr as $val){\n        global $ruta_actual;\n\n        if(is_dir($val) &amp;&amp; file_exists($val)){\n            global $total_directorio;\n            global $numero_fila;\n            $total_directorio = 0;\n\n            $ob=scandir($val);\n            foreach($ob as $file){\n                if($file=="."||$file==".."){\n                    continue;\n                }\n                $file=$val."/".$file;\n\n                if(is_file($file)){\n                    $count++;\n                    $suma_tamanho = $suma_tamanho + filesize($file)/1024;\n                    $acumulado_tamanho = $acumulado_tamanho + filesize($file)/1024;\n                    $total_directorio++;\n                } elseif(is_dir($file)){\n                    FileCount($file, $mostrar_hasta_nivel, $nivel_directorio_raiz);\n                }\n            }\n\n            $nivel_directorio = count(explode("/", $val)) - 1;\n\n            if ($nivel_directorio &gt; $mostrar_hasta_nivel) {\n            } else {\n                $atributo_fila = (($numero_fila%2)==1 ? "background-color:#ffffff;" : "background-color:#f2f2f2;");\n                echo "&lt;tr style=\'".$atributo_fila."\'&gt;&lt;td&gt;".$numero_fila."&lt;/td&gt;&lt;td&gt;".$val."&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;&lt;td align=\'center\'&gt;".$nivel_directorio."&lt;/td&gt;&lt;td align=\'right\' style=\'color:#0000ff;\'&gt;".number_format(($count - $count_anterior),0,",",".")."&lt;/td&gt;&lt;td align=\'right\'&gt;".number_format($count,0,",",".")."&lt;/td&gt;&lt;td align=\'right\'&gt;".number_format($total_directorio,0,",",".")."&lt;/td&gt;&lt;td align=\'right\' style=\'color:#0000ff;\'&gt;".number_format($suma_tamanho,0,",",".")." Kb.&lt;/td&gt;&lt;td align=\'right\'&gt;".number_format($acumulado_tamanho,0,",",".")." Kb.&lt;/td&gt;&lt;/tr&gt;";\n\n                $count_anterior = $count;\n                $suma_tamanho = 0;\n                $numero_fila++;\n            }\n\n        }\n    }\n}\n?&gt;\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/29746', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/115/']