1A cleaner approach:
2
3function Size($path)
4{
5 $bytes = sprintf('%u', filesize($path));
6
7 if ($bytes > 0)
8 {
9 $unit = intval(log($bytes, 1024));
10 $units = array('B', 'KB', 'MB', 'GB');
11
12 if (array_key_exists($unit, $units) === true)
13 {
14 return sprintf('%d %s', $bytes / pow(1024, $unit), $units[$unit]);
15 }
16 }
17
18 return $bytes;
19}