Output File Size in PHP

Pass a file size to this function like this:

$filesize = get_file_size(2048);

And return a nicely formatted file size:

2 MB

// function to get file size and present in human readable format
function get_file_size($size) {
	$sizes = Array(' B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB');
	$ext = $sizes[0];
	for ($i=1; (($i = 1024)); $i++) {
		$size = $size / 1024;
		$ext  = $sizes[$i];
	}
	return number_format($size)." ".$ext;
}

Leave a Reply