Advertisement
Guest User

byte helper

a guest
Feb 17th, 2018
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. /**
  2.  * Gets max upload size from post_max_size and upload_max_filesize
  3.  * Returns whichever is smaller
  4.  *
  5.  * @return int
  6.  */
  7. function file_upload_max_size() {
  8.     $max_size = convert_to_bytes(ini_get('post_max_size'));
  9.     $upload_max = convert_to_bytes(ini_get('upload_max_filesize'));
  10.     if ($upload_max > 0 && $upload_max < $max_size) {
  11.         $max_size = $upload_max;
  12.     }
  13.     return $max_size;
  14. }
  15.  
  16. /**
  17.  * Converts KB (K) through GB (G) to bytes
  18.  *
  19.  * @param string $from
  20.  * @return int bytes
  21.  */
  22. function convert_to_bytes($from) {
  23.     $number = filter_var($from, FILTER_SANITIZE_NUMBER_INT);
  24.     if (empty($number)) {
  25.         return 0;
  26.     }
  27.     $type = strtoupper(str_replace($number, '', $from));
  28.     switch ($type) {
  29.         case "KB":
  30.         case "K":
  31.             $number = $number * 1024;
  32.             break;
  33.         case "MB":
  34.         case "M":
  35.             $number = $number * pow(1024, 2);
  36.             break;
  37.         case "GB":
  38.         case "G":
  39.             $number = $number * pow(1024, 3);
  40.             break;
  41.         default:
  42.             return 0;
  43.     }
  44.     return fix_integer_overflow($number);
  45. }
  46.  
  47. /**
  48.  * Converts bytes to KB (K) through GB (G)
  49.  *
  50.  * @param int $bytes
  51.  * @param string $type Type to convert to
  52.  *                     KB (K) through GB (G)
  53.  * @return int bytes
  54.  */
  55. function convert_bytes_to_type($bytes, $type = 'MB') {
  56.     $number = filter_var($bytes, FILTER_SANITIZE_NUMBER_INT);
  57.     if (empty($number)) {
  58.         return 0;
  59.     }
  60.     $type = strtoupper(str_replace($number, '', $type));
  61.     switch ($type) {
  62.         case "KB":
  63.         case "K":
  64.             $divisor = 1024;
  65.             break;
  66.         case "MB":
  67.         case "M":
  68.             $divisor = pow(1024, 2);
  69.             break;
  70.         case "GB":
  71.         case "G":
  72.             $divisor = pow(1024, 3);
  73.             break;
  74.         default:
  75.             return 0;
  76.     }
  77.     return $bytes / $divisor;
  78. }
  79.  
  80. /**
  81.  * Converts bytes into human readable form
  82.  *
  83.  * @param string || File
  84.  * @param int precision round precision
  85.  * @return int rounded bytes in units
  86.  */
  87. function human_readable_bytes($bytes, $precision = 2) {
  88.     $CI = & get_instance();
  89.     $CI->lang->load('number');
  90.     if (is_file($bytes)) {
  91.         $bytes = fix_integer_overflow(filesize($bytes));
  92.     }
  93.     $units = array(
  94.         $CI->lang->line('bytes'),
  95.         $CI->lang->line('kilobyte_abbr'),
  96.         $CI->lang->line('megabyte_abbr'),
  97.         $CI->lang->line('gigabyte_abbr'),
  98.         $CI->lang->line('terabyte_abbr')
  99.     );
  100.     $bytes = max($bytes, 0);
  101.     $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
  102.     $pow = min($pow, count($units) - 1);
  103.     $bytes /= pow(1024, $pow);
  104.     return round($bytes, $precision) . ' ' . $units[$pow];
  105. }
  106.  
  107. /**
  108.  * Fixes integer overflow
  109.  *
  110.  * @param int $size
  111.  * @return int
  112.  */
  113. function fix_integer_overflow($size) {
  114.     if ($size < 0) {
  115.         $size += 2.0 * (PHP_INT_MAX + 1);
  116.     }
  117.     return $size;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement