Tuesday, July 13, 2010

PHP function to calculate the size of folder/directory

Posted by admin on 4:12 AM 0 comments

PHP function to calculate the size of folder/directory

Here is the PHP function to calculate the size of folder/directory.

This function gives output in bytes but i have modified it to output in KB, MB and GB also.

You can use this simple PHP function to calculate the size of folder/directory and enjoy.




// function to calculate the size of folder/directory, output comes in bytes

function filesize_r($path){

if(!file_exists($path)) return 0;

if(is_file($path)) return filesize($path);

$ret = 0;

foreach(glob($path."/*") as $fn)

$ret += filesize_r($fn);

return $ret;

}





$path = "uploads/user_videos/43"; // path of folder

echo "Folder $path size is = ".filesize_r($path)." bytes.
";



if(filesize_r($path)>1024)

{

$folder_size_KB = filesize_r($path)/1024; // size in kilobytes

echo "Folder $path size is = ".number_format($folder_size_KB, 2)." kilobytes(KB)
";

}

if($folder_size_KB>1024)

{

$folder_size_MB = $folder_size_KB / 1024; // size in megabytes

echo "Folder $path size is = ".number_format($folder_size_MB, 2)." megabytes(MB)
";

}

if($folder_size_MB>1024)

{

$folder_size_GB = $folder_size_KB / 1024; // size in megabytes

echo "Folder $path size is = ".number_format($folder_size_GB, 2)." megabytes(MB)
";

}

?>


Tags: Funny Pictures, Pakistani Girls Pictures Indian Teen Girls, Sports Celebrities Pictures, Hollywood Celebrities, Bollywood Celebrities, Katrina Kaif, South Indian Actress, Hot Teen Models


0 Responses so far:

Leave a Reply