DooPHP IRC channel


File management with DooFile

This is for general DooPHP discussion. Ask your questions here.

Re: File management with DooFile

Postby ximian » Thu Jan 07, 2010 8:04 am

leng wrote:but filesize only works for files, so would need to recursively get all the file size in the folder and get the sum out of it.

That's why i said that we should have a Directory class to handle all directory stuff. This way you can group all directory functions together and eg, when you do a directory listing cycle, that method could be smart to return objects (of type directory, file, image) instead of just strings. This way you could apply some tasks recursively.
Anyway, here goes the directory::getSize

Code: Select all
        function getSize( $recursive = false, $formatted = false, $decimals = 1 ) {
            $total = 0;
            $dirHandle = opendir( $this->getPath() );
            while ( false !== ( $file = readdir( $dirHandle ) ) ) {
                if ( $file == '.' || $file == '..') {
                    continue;
                }
                if ( is_file( $this->getPath() .'/'. $file ) ) {
                    $total += filesize( $this->getPath() .'/'. $file );
                } else if ( $recursive && is_dir( $this->getPath() .'/'. $file ) ) {
                    $subdir = new Directory( $this->getPath() .'/'. $file );
                    $total += $subdir->getSize(true, false, $decimals);
                }
            }
            closedir( $dirHandle );
            if ( ! $formatted ) {
                return $total;
            }
            return Util::formatFilesize( $total, $decimals );
        }

        function getPath() {
            return Path::getFullPath( $this->_path );
        }


Code: Select all
        // Path class
        function getFullPath( $path ) {
            return realpath( $path );
        }


Francisco A
ximian
 
Posts: 143
Joined: Wed Nov 25, 2009 1:51 am
Location: Portugal

Re: File management with DooFile

Postby leng » Thu Jan 07, 2010 9:53 am

Not sure what you meant but I added getSize() where you can choose what unit(B, KB, MB, GB) to be return. And a static formatBytes() to convert bytes to other units.
Code: Select all
    /**
     * Get the space used up by a folder recursively.
     * @param string $dir Directory path.
     * @param string $unit Case insensitive units: B, KB, MB, GB or TB
     * @param int $precision
     * @return float total space used up by the folder (KB)
     */
    public function getSize($dir, $unit='KB', $precision=2){
        if(!is_dir($dir)) return filesize($dir);
        $dir = str_replace('\\', '/', $dir);
        if($dir[strlen($dir)-1] != '/'){
            $dir .= '/';
        }

        $totalSize = 0;
      $handle = opendir($dir);

      while(false !== ($file = readdir($handle))){
         if($file != '.' && $file != '..'){
                if (is_dir($dir.$file)){
               $totalSize += $this->getSize($dir.$file);
            }else{
                    $totalSize += filesize($dir.$file);
            }
         }
      }
      closedir($handle);
        return self::formatBytes($totalSize, $unit, $precision);
    }
Just Doo IT!
leng
 
Posts: 1482
Joined: Thu Jul 16, 2009 11:33 pm

Previous

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 1 guest