Jump to content

Folder Size


Dysan

Recommended Posts

I made a script for that:

<?php
   /*
      author:   Benjamin Falk
      mail:   falk  [at]   citrosaft.com
      
      you can use this script,
      change and  copy it,  as
      you wish.
   */
   
   global $humanity, $verbose;
   $argv = $_SERVER['argv'];
   $dir = '';
   
   $humanity   = false;
   $verbose   = false;
   $help      = false;
   foreach ($argv as $a) { //read arguments
      if (is_file($a)) continue;
      elseif ($a == '--help') $help = true;
      elseif (substr($a, 0,1) == '-') {
         $o = str_split(substr($a, 1)); // split arguments
         foreach ($o as $i) {
            if ($i == 'h') $humanity = true;
            elseif ($i == 'v') $verbose = true;
            elseif ($i == '?') $help = true;
            else die("Unknown argument $i\n");
         }
      }elseif (is_dir($a) && file_exists($a)) $dir = $a;
   }
   
   /* Help-Section */
   if ($help === true) {
      echo "Usage: php ".basename(__FILE__)." [OPTION]... [DIRECTORY]\n";
      echo "Scanns directories and outputs the total size and total numbers of directories and files.\n\n";
      
      
      echo "  -v\t\tVerbose mode. Shows you which directory get scanned right now.\n";
      echo "  -h\t\tThe result gets formated better\n";
      echo "  -?, --help\tOuputs this text\n\n";
      
      exit;
   }
   /* end of Help-Section */
   
   if ($dir == '') $dir = dirname(__FILE__);
   echo "Reading $dir...\n";
   $res = getSize($dir); //start scanning

   if ($humanity === true) {
      $size = $res[0];
      $e = array('Bytes', 'KB', 'MB', 'GB', 'TB');
      $i=0;
      while ($size >= 1024 && $i < count($e)) {
         $i++;
         $size /= 1024;
      }
      $size = round($size, 2);
      
      $fn1 = 'Size';
      $fn2 = 'Directories';
      $fn3 = 'Files';
      
      $margin = 5;
      $f1 = strlen(strval($size).' '.$e[$i]);
      if ($f1 < strlen($fn1)) $f1 = strlen($fn1);
      $f2 = strlen(strval($res[1]));
      if ($f2 < strlen($fn2)) $f2 = strlen($fn2);
      $f3 = strlen(strval($res[2]));
      if ($f3 < strlen($fn3)) $f3 = strlen($fn3);
      
      echo str_pad($fn1,$f1,' ',STR_PAD_LEFT).
          str_repeat(' ',$margin).
          str_pad($fn2,$f2,' ',STR_PAD_LEFT).
          str_repeat(' ',$margin).
          str_pad($fn3,$f3,' ',STR_PAD_LEFT)."\n";
      echo str_pad(strval($size).' '.$e[$i],$f1,' ',STR_PAD_LEFT).
          str_repeat(' ',$margin).
          str_pad(strval($res[1]),$f2,' ',STR_PAD_LEFT).
          str_repeat(' ',$margin).
          str_pad(strval($res[2]),$f3,' ',STR_PAD_LEFT)."\n";
   }else echo "Size\t\tDirectories\t\tFiles\n{$res[0]}\t\t{$res[1]}\t\t{$res[2]}\n";
   
   /* Main-Function */
   function getSize($dir) {
      global $verbose;
      
      $cur_size = 0;
      $cur_files =0;
      $cur_dirs = 0;
      
      if (substr($dir,-1) != '/') $dir .= '/';
      $dir .= '*';
      $d = glob($dir);
      
      foreach ($d as $item) {
         if (is_dir($item)) {
            $cur_dirs++;
            if ($verbose === true) echo "Reading $item...\n";
            $res = getSize($item);
            $cur_size   += $res[0];
            $cur_dirs   += $res[1];
            $cur_files   += $res[2];
            if ($verbose === true) echo "   Result of $item:\n".
                                 "      Size: {$res[0]}\tDirectories: {$res[1]}\tFiles: {$res[2]}\n";
         }else {
            $cur_files++;
            $cur_size += filesize($item);
         }
      }
      
      return array($cur_size, $cur_dirs, $cur_files);
   }
   /* end of Main-Function */
?>

I wrote it for the php-commandline, but you can change some lines and then you can use it for your webstuff.

 

For your use the function getSize($dir) is important. Just call the function like

$dirres = getSize('/var/www');

It returns an array and it is buildt up as

Array {
   [0] Size in bytes
   [1] Count of directories
   [2] Count of files
}

Link to comment
Share on other sites

Is it possible to find a display the size of a folder contained in the HTDOCS Folder?

 

<?php
// $df contains the total number of bytes available on "/"
$df = disk_total_space("/");

// On Windows:
disk_total_space("C:");
disk_total_space("D:");
?>

Link to comment
Share on other sites

Umm, that function will return the disk/partition size the directory is located on. It doesn't calculate the size of the directory. Looking through my old scripts I found this:

<?php

/* Disclamper:
    The following function  was taken from http://uk3.php.net/manual/en/function.disk-total-space.php#75971 */
function getSymbolByQuantity($bytes) {
    $symbols = array('Bytes', 'KB', 'MB', 'GB');
    $exp = floor(log($bytes)/log(1024));

    return sprintf('%.2f '.$symbols[$exp], ($bytes/pow(1024, floor($exp))));
}
/* End Disclaimer */

function folder_size($path='.', $dirSize=0)
{
    if(!is_dir($path))
        die('Path (' .  $path . ') is invalid');

    $handle = opendir($path);

    $ignoreFiles = array('.', '..');

    while ( false !== ($file = readdir($handle)))
    {
        if (!in_array($file, $ignoreFiles))
        {
            $filePath = $path . '/' . $file;

            if(is_dir($filePath))
            {
                $dirSize = folder_size($filePath, $dirSize);
            }
            else
            {
                $dirSize += filesize($filePath);
            }
        }
    }

    closedir($handle);

    return $dirSize;
}

$folder_size = getSymbolByQuantity(folder_size('.'));


echo 'Total folder size = ' . $folder_size;

?>

Note: getSymbolByQuantity function is not mine.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.