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);

}

The function seems foldersize_formatted() seems to be working fine.Tried large numbers and it worked(always got the corresponding suffix).Just for debugging echo the $total_size variable before passing it to the function.As far as I know,PHP doesn't have a concept of overflow(not for numbers in the range we are dealing with here).The only way you get bytes at the end is if your input is less than 1024 (-ve ?)

Firstly, thanks for the reply. This is the strange part I am not understanding as I have a folder which is 600mb. When passing it through the foldersize() i get the correct result (in bytes). But when passing the value to folder_size_formatted() the value is converted to MB correctly(600mb) but 'bytes' still gets concatinated to the variable.

So I end up with '600 Bytes'!

It should only add 'bytes' if the value is lower than 1024...but its not for some reason....

There is a little flaw in your foldersize() function.It returns the size of only files immediately inside it.If there are nested subdirectories,then it fails. Could that be a problem as to why you are getting an incorrect result(Though you already mentioned that you get the correct value before passing the value to your formatting function)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.