Hello! I wonder if anyone knows how to get the total size of all files in a specified directory to a variable. Kalle
$dir = 'some_dir/*'; $size = array_sum(array_map('filesize', glob($dir))); echo $size; PHP: Untested. EDIT: And you can use this to get the size of the subdirs as well. function get_dir_size($directory, $subdirs = true) { $files = glob($directory .'*'); $totalsize = 0; foreach ($files AS $file) { if ($subdirs AND is_dir($file)) { $totalsize += get_dir_size($file .'/'); } else { $totalsize += filesize($file); } } return $totalsize; } echo get_dir_size('some-dir/'); PHP:
Thanks a lot. I found another script that I am using though. It is the following: <?php function getDirSize( $path ) { $totsize = 0; $dp = opendir( $path ); while( $child = readdir( $dp ) ) { if( $child == '.' || $child == '..' ) continue; if( is_dir( $path . '/' . $child ) ) { $totsize += getDirSize( $path . '/' . $child ); } else { $totsize += filesize( $path . '/' . $child ); } } return $totsize; } $totalsize = 0; $totalsize = round((getDirSize('images')/1024)/1024, 2); echo $totalsize; ?> Code (markup):
This line: while( $child = readdir( $dp ) ) { PHP: Should look like this: while( ( $child = readdir( $dp ) ) !== false ) PHP: http://us2.php.net/readdir