Zend Framework Source Code Snippets

Humanize bytes view helper

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
class Zend_View_Helper_Humanize
{
  public function humanize($bytes)
  {
    $orig = $bytes;    $ext = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
    $unitCount = 0;
 
    for(; $bytes > 1024; $unitCount++) $bytes /= 1024;
     $human1 = sprintf("%s %s", round($bytes, 2), $ext[$unitCount]);
    $human2 = sprintf("%s %s", $orig, $ext[0]);
    
    return $human1 == $human2 ? $human1 : sprintf("%s (%s)", $human1, $human2);  
  }}

Comments

umpirsky umpirsky
1 year ago

Nice work. There is Zend_Measure_Binary as well ;)

You must login before commenting on a snippet. If you do not have an account, please register.

Snippet description

Humanize bytes to GB, KB, etc

usage: echo $this->humanize(1572864);

Snippet details

Created:
raspi raspi
1 year ago
Edited:
raspi raspi
1 year ago
Revision Id:
23
Edit Message:
Initial Release
Tags:
view helper bytes file size
Comments:
1
Views:
69
Points:
0 (0 votes)

History

r23

Initial Release

raspi raspi
1 year ago