PHP ver gevorderde |
|
Nou, gebruiken jullie dit topic maar om offtopic te zijn (zoals jullie al doen).
Ik heb de oplossing namelijk al (ook al is hij wat omslagtig).
<?php
error_reporting(E_ALL);
function dirsize($dir,$scan_subdirs = true) {
$file_list = '';
$size = '';
$stack[] = $dir;
while ($stack) {
$current_dir = array_pop($stack);
if ($dh = opendir($current_dir)) {
while (($file = readdir($dh)) !== false) {
if ($file !== '.' && $file !== '..') {
$current_file = "{$current_dir}/{$file}";
if (is_file($current_file)) {
if($scan_subdirs){
$size = $size + filesize("{$current_dir}/{$file}");
}
else{
$size = $size + filesize($file);
}
}
elseif ($scan_subdirs && is_dir($current_file)) {
$stack[] = $current_file;
}
}
}
}
}
return $size;
}
function getfilesize($bytes) {
if ($bytes >= pow(2,40)) {
$return = round($bytes / pow(1024,4), 2);
$suffix = "TB";
} elseif ($bytes >= pow(2,30)) {
$return = round($bytes / pow(1024,3), 2);
$suffix = "GB";
} elseif ($bytes >= pow(2,20)) {
$return = round($bytes / pow(1024,2), 2);
$suffix = "MB";
} elseif ($bytes >= pow(2,10)) {
$return = round($bytes / pow(1024,1), 2);
$suffix = "KB";
} else {
$return = $bytes;
$suffix = "Byte";
}
if ($return == 1) {
$return .= " " . $suffix;
} else {
$return .= " " . $suffix . "s";
}
return $return;
}
echo getfilesize(dirsize("."));
?>
<?php function dirsize($dir,$scan_subdirs = true) { $file_list = ''; $size = ''; $stack[] = $dir; while ($stack) { while (($file = readdir($dh)) !== false) { if ($file !== '.' && $file !== '..') { $current_file = "{$current_dir}/{$file}"; if($scan_subdirs){ $size = $size + filesize("{$current_dir}/{$file}"); } else{ } } elseif ($scan_subdirs && is_dir($current_file)) { $stack[] = $current_file; } } } } } return $size; } function getfilesize($bytes) { if ($bytes >= pow(2,40)) { $return = round($bytes / pow(1024,4), 2); $suffix = "TB"; } elseif ($bytes >= pow(2,30)) { $return = round($bytes / pow(1024,3), 2); $suffix = "GB"; } elseif ($bytes >= pow(2,20)) { $return = round($bytes / pow(1024,2), 2); $suffix = "MB"; } elseif ($bytes >= pow(2,10)) { $return = round($bytes / pow(1024,1), 2); $suffix = "KB"; } else { $return = $bytes; $suffix = "Byte"; } if ($return == 1) { $return .= " " . $suffix; } else { $return .= " " . $suffix . "s"; } return $return; } echo getfilesize (dirsize (".")); ?>
Bron: PHP hulp - map_size |