1function toByteSize($p_sFormatted) {
2 $aUnits = array('B'=>0, 'KB'=>1, 'MB'=>2, 'GB'=>3, 'TB'=>4, 'PB'=>5, 'EB'=>6, 'ZB'=>7, 'YB'=>8);
3 $sUnit = strtoupper(trim(substr($p_sFormatted, -2)));
4 if (intval($sUnit) !== 0) {
5 $sUnit = 'B';
6 }
7 if (!in_array($sUnit, array_keys($aUnits))) {
8 return false;
9 }
10 $iUnits = trim(substr($p_sFormatted, 0, strlen($p_sFormatted) - 2));
11 if (!intval($iUnits) == $iUnits) {
12 return false;
13 }
14 return $iUnits * pow(1024, $aUnits[$sUnit]);
15}
1<?php
2// Snippet from PHP Share: http://www.phpshare.org
3
4 function formatSizeUnits($bytes)
5 {
6 if ($bytes >= 1073741824)
7 {
8 $bytes = number_format($bytes / 1073741824, 2) . ' GB';
9 }
10 elseif ($bytes >= 1048576)
11 {
12 $bytes = number_format($bytes / 1048576, 2) . ' MB';
13 }
14 elseif ($bytes >= 1024)
15 {
16 $bytes = number_format($bytes / 1024, 2) . ' KB';
17 }
18 elseif ($bytes > 1)
19 {
20 $bytes = $bytes . ' bytes';
21 }
22 elseif ($bytes == 1)
23 {
24 $bytes = $bytes . ' byte';
25 }
26 else
27 {
28 $bytes = '0 bytes';
29 }
30
31 return $bytes;
32}
33?>
34
1<?php
2 function tobytes($size, $type)
3 {
4 $types = array("B", "KB", "MB", "GB", "TB", "PB"); //You can add the rest if needed..
5
6 if($key = array_search($type, $types))
7 return $size * pow(1024, $key);
8 else return "invalid type";
9 }
10
11 echo tobytes(15, "MB"); //15728640
12 echo tobytes(2, "KB"); //2048
13 echo tobytes(3, "w/e"); //invalid type
14?>
15
1function formatBytes($bytes, $precision = 2) {
2 $units = array('B', 'KB', 'MB', 'GB', 'TB');
3
4 $bytes = max($bytes, 0);
5 $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
6 $pow = min($pow, count($units) - 1);
7
8 // Uncomment one of the following alternatives
9 // $bytes /= pow(1024, $pow);
10 // $bytes /= (1 << (10 * $pow));
11
12 return round($bytes, $precision) . ' ' . $units[$pow];
13}