math - Display File Sizes with the Appropriate Denomination in PHP -


using php, what's easy way display file sizes appropriate denomination in regard amount of bytes?

i came little function this:

function filesize_exact($isize, $iprecision = 2, $bspace = true) { // gabriel nahmias (gabriel@terrasoftlabs.com) // measure byte ranges in descending order. $sunit = "bytes"; if ($isize > 1024 * 1024 * 1024 * 1024 * 1024) { $sunit = "pb"; $isize /= 1024 * 1024 * 1024 * 1024 * 1024; } else if ($isize > 1024 * 1024 * 1024 * 1024) { $sunit = "tb"; $isize /= 1024 * 1024 * 1024 * 1024; } else if ($isize > 1024 * 1024 * 1024) { $sunit = "gb"; $isize /= 1024 * 1024 * 1024; } else if ($isize > 1024 * 1024) { $sunit = "mb"; $isize /= 1024 * 1024; } else if ($isize > 1024) { $sunit = "kb"; $isize /= 1024; } if ($iprecision > 0 && $iprecision != null) $isize = round($isize, $iprecision); // make sure bytes spaced out; changed. return $isize . ($bspace || $sunit == "bytes" ? " " : "") . $sunit; } 

works me. has couple of nice options. extend encapsulate range of sizes long it's in descending order because that's how logic set here , it's suitable. see http://www.webopedia.com/quick_ref/filesizeconversiontable.asp decent list of file size denominations.


Comments

Popular posts from this blog

javascript - backbone.js Collection.add() doesn't `construct` (`initialize`) an object -

php - Get uncommon values from two or more arrays -

Adding duplicate array rows in Php -