Created
          April 6, 2020 16:40 
        
      - 
      
- 
        Save ivo-ivanov/daafbd8a7d95100606ccb30ed6b89a25 to your computer and use it in GitHub Desktop. 
    Format bytes #php
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // format bytes in KB/MB | |
| function formatBytes($bytes, $precision = 0) { | |
| $units = array('B', 'KB', 'MB', 'GB', 'TB'); | |
| $bytes = max($bytes, 0); | |
| $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); | |
| $pow = min($pow, count($units) - 1); | |
| // Uncomment one of the following alternatives | |
| // $bytes /= pow(1024, $pow); | |
| $bytes /= (1 << (10 * $pow)); | |
| return round($bytes, $precision) . '' . $units[$pow]; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment