Guys Ive spent some time trying to see whats wrong but im getting nowhere....
Let me explin what I want to do. Firstly I want to obtain the whole size of a directory (foldersize function). Secondly, i want to pass that number (in bytes) to the foldersize_formatted function to show the number in bytes, mb etc...
Problem is i obtain the right size, but end up with 'bytes' instead on GB (example). I have found & tried other samples from the net that would replace my foldersize_formatted function, but they also give me the same result!
Can any guru guide me with this issue?
Regards,
Ryan
echo foldersize($fullpath);
function foldersize_formatted($size_bytes){
if ($size_bytes < 1024) {
$size_formatted = number_format($size_bytes, 2, '.', '');
$size_formatted = $size_formatted . ' Bytes';
return $size_formatted;
}
elseif ($size_bytes < 1048576) {
$size_formatted = $size_bytes / 1024 ;
$size_formatted = number_format($size_formatted, 2, '.', '');
$size_formatted = $size_formatted . ' KB';
return $size_formatted;
}
elseif ($size_bytes < 1073741824) {
$size_formatted = $size_bytes / 1048576 ;
$size_formatted = number_format($size_formatted, 2, '.', '');
$size_formatted = $size_formatted . ' MB';
return $size_formatted;
}
else {
$size_formatted = $size_bytes / 1073741824;
$size_formatted = number_format($size_formatted, 2, '.', '');
$size_formatted = $size_formatted . ' GB';
return $size_formatted;
}
}
function foldersize($path) {
$total_size = 0;
$files = scandir($path);
$cleanPath = rtrim($path, '/'). '/';
foreach($files as $t) {
if ($t<>"." && $t<>"..") {
$currentFile = $cleanPath . $t;
if (is_dir($currentFile)) {
$size = foldersize($currentFile);
$total_size += $size;
}
else {
$size = filesize($currentFile);
$total_size += $size;
}
}
}
return foldersize_formatted($total_size);
}