Zend Framework Source Code Snippets

View helper for printing file-size

Bookmark and Share
1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
29
/**
 * ViewHelper for printing human-readable file-sizes
 * @author Jakub Truneček <jakub@trunecek.net>
 */
class Zend_View_Helper_FileSize extends Zend_View_Helper_Abstract{
    /**
     * Acceptable prefices of SI
     * @var array 
     */    protected static $_prefixes = array('', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y');
    
    /**
     * Tranformation to huma-readable format
     * @param  int $size Size in bytes     * @param  int $precision Presicion of result (default 2)
     * @return string Transformed size
     */
    public function fileSize($size, $precision = 2)
    {                $result = $size;
        $index = 0; 
        while ($result > 1024 && $index < count(self::$_prefixes)) {
            $result = $result / 1024;
            $index++;        }
        return sprintf('%1.' . $precision . 'f %sB', $result, self::$_prefixes[$index]);
    }
}

Comments

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

Snippet description

Snippet details

Created:
Truneček Truneček
1 month ago
Edited:
Truneček Truneček
1 month ago
Revision Id:
159
Edit Message:
Initial Release
ZF Version
1.0.4
Tags:
view helper File size Bytes File SI
Comments:
0
Views:
1043
Points:
1 (1 votes)

History

r159

Initial Release

Truneček Truneček
1 month ago
diff
r158

Initial Release

Truneček Truneček
1 month ago
diff
r157

Initial Release

Truneček Truneček
1 month ago