Format the Byte Size of a File Using PHP

When using filesize() wrap it in the function to display the size in a nice format:

function format_bytes($size) {
$units = array(' B', ' KB', ' MB', ' GB', ' TB');
for ($i = 0; $size >= 1024 && $i < 4; $i++) $size /= 1024;
return round($size, 2).$units[$i];
}

Tags: , , ,

About Keiron

Web Developer based in the UK. Click here if you want to work with me