PHP get folder or file size

#bytes#directory#file#folder#kb#mb#size

This function returns the size in bytes for any path (folder/file).

<?php
/**
 * Get Folder/file size in bytes
*/
function getSize($path)
{
    $size = 0;
    if(is_file($path))
    	return filesize( $path );

    foreach (glob(rtrim($path, '/').'/*', GLOB_NOSORT) as $each) {
    	$size += is_file($each) ? filesize($each) : getSize($each);
    }
    return $size;
}

Format bytes into KB, MB, GB, …

<?php
/**
 * Format bytes
*/
function formatBytes($size, $precision = 0)
{
	$unit = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'); 

	for($i = 0; $size >= 1024 && $i < count($unit)-1; $i++){
        $size /= 1024;
	}

	return round($size, $precision).' '.$unit[$i];
}