how to check folder size

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • exoskeleton
    New Member
    • Sep 2006
    • 104

    how to check folder size

    hi dear experts,

    hope you can help about checking the folder size,for example how many kilobytes is it already in PHP script...is it possible?

    please help..thank you
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    This little snippet, by Jonas Sweden in the PHP documentation, adds the sizes of all files in the folder and show the total bytes.
    [code=php]<?php
    $path = "gal";
    echo "Folder $path = ".filesize_r($p ath)." bytes";

    function filesize_r($pat h){
    if(!file_exists ($path)) return 0;
    if(is_file($pat h)) return filesize($path) ;
    $ret = 0;
    foreach(glob($p ath."/*") as $fn)
    $ret += filesize_r($fn) ;
    return $ret;
    }
    ?>[/code]
    Ronald :cool:
    Last edited by pbmods; Feb 5 '09, 11:01 PM. Reason: Fixed CODE tags.

    Comment

    • exoskeleton
      New Member
      • Sep 2006
      • 104

      #3
      thank you very much sir ronverdonk...i will try the code right away...

      OT: i read some threads, hehe...you like motoma? peace!!! :)

      Comment

      • exoskeleton
        New Member
        • Sep 2006
        • 104

        #4
        Originally posted by ronverdonk
        This little snippet, by Jonas Sweden in the PHP documentation, adds the sizes of all files in the folder and show the total bytes.
        [code=php]<?php
        $path = "gal";
        echo "Folder $path = ".filesize_r($p ath)." bytes";

        function filesize_r($pat h){
        if(!file_exists ($path)) return 0;
        if(is_file($pat h)) return filesize($path) ;
        $ret = 0;
        foreach(glob($p ath."/*") as $fn)
        $ret += filesize_r($fn) ;
        return $ret;
        }
        ?>[/code]
        Ronald :cool:
        thank you thank you thank you sir ronverdonk...IT WORKS!!! :) good luck about..... :)
        Last edited by pbmods; Feb 5 '09, 11:01 PM. Reason: Fixed CODE tags.

        Comment

        • KINGMAX
          New Member
          • Feb 2008
          • 1

          #5
          Code works fantastically!

          Depending on the size of a folder, is it possible to display the size value in MegaBytes, GigaBytes, or TeraBytes, including the appropriate abbreviations MB, GB, TB

          Comment

          • Markus
            Recognized Expert Expert
            • Jun 2007
            • 6092

            #6
            Originally posted by KINGMAX
            Code works fantastically!

            Depending on the size of a folder, is it possible to display the size value in MegaBytes, GigaBytes, or TeraBytes, including the appropriate abbreviations MB, GB, TB
            Divide the bytes by 1024 to get kilobytes
            [code=php]
            $_bytes = $_FILES['file']['size'];
            echo ($_bytes / 1024) . "Kb";
            [/code]
            Last edited by pbmods; Feb 5 '09, 11:01 PM. Reason: Fixed CODE tags.

            Comment

            • ronverdonk
              Recognized Expert Specialist
              • Jul 2006
              • 4259

              #7
              Originally posted by KINGMAX
              Code works fantastically!

              Depending on the size of a folder, is it possible to display the size value in MegaBytes, GigaBytes, or TeraBytes, including the appropriate abbreviations MB, GB, TB
              Thanks you KINGMAX. That's what we are here for.
              And to wrap it up: here a little function that shows you the size in Gb, Mb, Kb or plain bytes.You can work out the Terabytes for yourself.[code=php]function showSize($size_ in_bytes) {
              $value = 0;
              if ($size_in_bytes >= 1073741824) {
              $value = round($size_in_ bytes/1073741824*10)/10;
              return ($round) ? round($value) . 'Gb' : "{$value} gB";
              } else if ($size_in_bytes >= 1048576) {
              $value = round($size_in_ bytes/1048576*10)/10;
              return ($round) ? round($value) . 'Mb' : "{$value} mB";
              } else if ($size_in_bytes >= 1024) {
              $value = round($size_in_ bytes/1024*10)/10;
              return ($round) ? round($value) . 'Kb' : "{$value} kB";
              } else {
              return "$size_in_b ytes bytes";
              }
              }[/code]Ronald
              Last edited by pbmods; Feb 5 '09, 11:01 PM. Reason: Fixed CODE tags.

              Comment

              • 7effrey
                New Member
                • Feb 2008
                • 12

                #8
                Originally posted by ronverdonk
                Thanks you KINGMAX. That's what we are here for.
                And to wrap it up: here a little function that shows you the size in Gb, Mb, Kb or plain bytes.You can work out the Terabytes for yourself.[code=php]function showSize($size_ in_bytes) {
                $value = 0;
                if ($size_in_bytes >= 1073741824) {
                $value = round($size_in_ bytes/1073741824*10)/10;
                return ($round) ? round($value) . 'Gb' : "{$value} gB";
                } else if ($size_in_bytes >= 1048576) {
                $value = round($size_in_ bytes/1048576*10)/10;
                return ($round) ? round($value) . 'Mb' : "{$value} mB";
                } else if ($size_in_bytes >= 1024) {
                $value = round($size_in_ bytes/1024*10)/10;
                return ($round) ? round($value) . 'Kb' : "{$value} kB";
                } else {
                return "$size_in_b ytes bytes";
                }
                }[/code]Ronald
                how will the whole script looks like?
                Last edited by pbmods; Feb 5 '09, 11:02 PM. Reason: Fixed CODE tags.

                Comment

                • ronverdonk
                  Recognized Expert Specialist
                  • Jul 2006
                  • 4259

                  #9
                  What script? This is just a sample you can use in your own application to show the size of folders. You'll have to write the script, in which you want to use it, yourself.

                  Ronald

                  Comment

                  • jorgerosa
                    New Member
                    • Aug 2008
                    • 1

                    #10
                    Originally posted by 7effrey
                    how will the whole script looks like?
                    [code=PHP]
                    <?php
                    $path = "myTargetedFold erNameHere"; // <-- Edit: Add your folder name here!

                    function filesize_r($pat h){ // Function 1
                    if(!file_exists ($path)) return 0;
                    if(is_file($pat h)) return filesize($path) ;
                    $ret = 0;
                    foreach(glob($p ath."/*") as $fn)
                    $ret += filesize_r($fn) ;
                    return $ret;
                    }

                    function showSize($size_ in_bytes) { // Function 2
                    $value = 0;
                    if ($size_in_bytes >= 1073741824) {
                    $value = round($size_in_ bytes/1073741824*10)/10;
                    return ($round) ? round($value) . 'Gb' : "{$value} Gb";
                    } else if ($size_in_bytes >= 1048576) {
                    $value = round($size_in_ bytes/1048576*10)/10;
                    return ($round) ? round($value) . 'Mb' : "{$value} Mb";
                    } else if ($size_in_bytes >= 1024) {
                    $value = round($size_in_ bytes/1024*10)/10;
                    return ($round) ? round($value) . 'Kb' : "{$value} Kb";
                    } else {
                    return "{$size_in_byte s} Bytes";
                    }
                    }

                    echo "Folder {$path} size: <b>".showSize(f ilesize_r($path ))."</b>";
                    ?>
                    [/code]
                    Last edited by pbmods; Feb 5 '09, 11:02 PM. Reason: Fixed CODE tags.

                    Comment

                    • JSusi
                      New Member
                      • Feb 2009
                      • 1

                      #11
                      I found this in the web one day and it works fine for me.
                      [code=php]
                      function getSymbolByQuan tity($bytes)
                      {
                      $symbols = array('B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb', 'Zb', 'Yb');
                      $exp = floor(log($byte s)/log(1024));

                      return sprintf("%.2f " . $symbols[$exp], ($bytes/pow(1024, floor($exp))));
                      }[/code]
                      Last edited by pbmods; Feb 5 '09, 11:02 PM. Reason: Fixed CODE tags.

                      Comment

                      Working...