check folders size and set the size limited

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • haktowr
    New Member
    • Apr 2013
    • 1

    #1

    check folders size and set the size limited

    helloo guy i want make script

    to check the folder size

    and set the folder max size 1GB

    i try to make it
    im useing linux debian


    root@de1:/home/unixuser/OGP_User_Files# dir
    user1 user2


    ===========

    the problem the users can upload more then 1 GB

    i want set max user folder to 1 GB only

    how i make this with php?

    im new in php and linux


    my code have problems

    i want check the folders size in dirname OGP_User_Files
    and if the folder size 1 GB chmod it to 444
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    You can use the DirectoryIterat or class to go over every file in a directory and find their sizes. Like this:
    Code:
    /**
     * Returns the total number of bytes for all files under the given directory.
     * @param string $path Path to the directory.
     * @return int
     */
    function getDirectorySize($path) {
        $size = 0;
        $directory = new DirectoryIterator($path);
        foreach ($directory as $file) {
            if ($file->isFile()) {
                $size += $file->getSize();
            }
            else if (!$file->isDot() && $file->isDir()) {
                $size += getDirectorySize($file->getPathname());
            }
        }
    
        return $size;
    }
    However, if you are making some sort of file sharing/storing site with multiple users, each of which can store an X amount of data, then I wouldn't recommend this approach. It would be easier to manage this if you didn't store the user files separately, but simply saved them all in a common place and stored the paths to the files in a database. Then you could calculate how much space a user is using by doing a database query instead of having to read it from the actual hard drive.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      if there are subfolders to check, the recursive version is a bit more convenient
      Code:
      /**
       * Returns the total number of bytes for all files under the given directory.
       * @param string $path Path to the directory.
       * @return int
       */
      function getDirectorySize($path) {
          $size = 0;
          $directory = new RecursiveIteratorIterator(
              new RecursiveDirectoryIterator(
                  $path, FilesystemIterator::SKIP_DOTS
              )
          );
      
          foreach ($directory as $file) {
              $size += $file->getSize();
          }
       
          return $size;
      }

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        O yea, there was a recursive version of that iterator. Nice catch. I completely overlooked it.

        Comment

        Working...