Zend - The PHP Company




Files and Directories

Add Code


format_filesize  

Type: code fragment
Added by: danielsan
Entered: 03/02/2002
Last modified: 02/12/2001
Rating: - (fewer than 3 votes)
Views: 4649
format_filesize choses the best human readable unit for $bytes returns a formatted string


<?php
/*
 *  format_filesize choses the best human readable unit for $bytes returns a formatted string
 *  Updates, documentation and examples: http://daniel.lorch.cc/projects/format_filesize/
 *
 *  Revision: 1.00
 */

function format_filesize($bytes$decimals 1)
{
    
$units = array(
      
'1152921504606846976' => 'EB'/* Exa Byte  10^18 */
      
'1125899906842624'    => 'PB'/* Peta Byte 10^15 */
      
'1099511627776'       => 'TB',
      
'1073741824'          => 'GB',
      
'1048576'             => 'MB',
      
'1024'                => 'KB'
    
);

    if(
$bytes <= 1024)
      return 
$bytes " Bytes";
    
    foreach(
$units as $base => $title)
      if(
floor($bytes $base) != 0)
        return 
number_format($bytes $base$decimals".""'") . ' ' $title;
}

?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories