Jump to:

17452 Posts in 4473 Topics by 1971 members


SilverStripe Forums » Archive » get the size of a folder

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Page: 1
Go to End
Author Topic: 1190 Views
  • MIke
    Community Member
    7 Posts

    get the size of a folder Link to this post

    I have modify my folder.php in sapphire/filesystem/Folder
    to be able to get the size of a folder directly with a folder object, I have ovverrides the function getAbsoluteSize of file

    this is called reccursivily ion case of folder inside the folder..

    //return the size in octet
       public function getAbsoluteSize($path='')
             $path = Director::baseFolder() . '/' . $this->getRelativePath();
          $h = @opendir($path);
          if($h==0)return 0;
          $sf = 0;
          while ($f = readdir($h))
             if ( $f!= "..")
                $nd = $path ."/". $f ;      
                if( $f!=".")
                   $sf += filesize( $nd );
                if( $f!="." && is_dir($nd))
                   $nd = $path . $f ;
                   $sf+= $this->getAbsoluteSize( $nd );
          return $sf ;

    For information, you can do it also with a shell but this requires 751 CHmod on the folder (more access..less security)

    $folder = "home/tea/mail/";

    if($pass === "eggnut") {
    $job = shell_exec("du -s -k $folder"); //summary
    echo "<b>SIZE in KB:</b> $job";

    I do not like to change my core, for the further versionning.
    But I do not need another class folder, and actually $Myfolder->getAbsoluteSize() exist and does not give the good result.

    If a guy of the core dev read this post do you think,; it culd be a good think to integreta this to the further versions.

    Your framework is awesome guys !

    10 of people , developers & others


  • Sean
    Forum Moderator
    921 Posts

    Re: get the size of a folder Link to this post

    Hi there,

    Thanks for the kind words of using SilverStripe, I hope development has been smooth for you and your team.

    Thanks also for showing the function you have created for Folder.php. The typical work flow for a community member wishing to contribute code to the main branch is to submit a patch, using our open source tracker.

    You can do that by visiting http://open.silverstripe.com, creating an account (if you haven't already), and hitting the "New ticket" button. You'll also want to select "Patch" from the Type dropdown, so we can recognise your ticket as a patch. From then, you'll probably see some comments in the ticket from a core developer if there are any questions about your code, or more information required.



Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.