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
