×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Artur Słaboszewski
Added: Aug 1, 2017 8:57 AM
Modified: Aug 1, 2017 9:30 AM
Views: 2466
  1. <?php
  2.     /**
  3.      * Converts the size in bytes to a more readable size
  4.      * @param integer $size
  5.      * @param string $unit
  6.      * @return string
  7.      */
  8.     static function strHumanFileSize( $size, $unit = "" ) {
  9.         if ( (!$unit && $size >= 1 << 30) || $unit == "GB" )
  10.             return number_format( $size / (1 << 30), 2 ) . "GB";
  11.         if ( (!$unit && $size >= 1 << 20) || $unit == "MB" )
  12.             return number_format( $size / (1 << 20), 2 ) . "MB";
  13.         if ( (!$unit && $size >= 1 << 10) || $unit == "KB" )
  14.             return number_format( $size / (1 << 10), 2 ) . "KB";
  15.         return number_format( $size ) . " bytes";
  16.  
  17.     }
  18. ?>