Skip to content

Commit

Permalink
readable size helper
Browse files Browse the repository at this point in the history
  • Loading branch information
markvaneijk committed May 9, 2024
1 parent afca999 commit 250aa71
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

if (! function_exists('readable_size')) {
function readable_size($bytes, $decimals = 2, $decimal_separator = ',', $thousands_separator = '.')
{
$base = log($bytes) / log(1024);
$unit = ['bytes', 'kb', 'mb', 'gb', 'tb', 'zb', 'pb'];
$unit = $unit[floor($base)];
$size = pow(1024, $base - floor($base));

if ($unit == 'bytes') {
$decimals = 0;
}

return number_format($size, $decimals, $decimal_separator, $thousands_separator).' '.$unit;
}
}

0 comments on commit 250aa71

Please sign in to comment.