Question

In PHP, how to convert file size into human readable string?


Annonce
Ad

Answer

function human_filesize($bytes, $decimals = 2)
{
	$size = array('B','kB','MB','GB','TB','PB','EB','ZB','YB');
	$factor = floor((strlen($bytes) - 1) / 3);
	return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$size[$factor];
}

// Example : displays 1.48MB
echo human_filesize(1550369);


[source]
# ID Query