Function to format bytes in human readable format

jhbalaji 0 Tallied Votes 160 Views Share

I was digging through my old codes to get this, still could not find. Hence when I wrote it, just made a note here such that later it wont be difficult to find atleast for me.

Format filesize, php filesize human readable

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