Home >Backend Development >PHP Tutorial >All-purpose byte unit conversion function

All-purpose byte unit conversion function

WBOY
WBOYOriginal
2016-07-25 08:49:501334browse
Capacity unit calculation supports defining the decimal retention length; defining the starting and target units, or automatically carrying according to 1024
                 
                                                                                                                                                                                                                                                                                                                  


class Util {
    /**
  1.           * Capacity unit calculation, supports the definition of decimal retention length; defines the starting and target units, or automatically carries by 1024
  2.                                                                                                                                                                                   ’ Capacity unit’s Bytes
  3. * @param type $decimals, the number of digits retained after the decimal point, one digit is reserved by default
  4. * @param type $targetUnit, the target unit of conversion, automatic carry by default
  5. * @return type Returns the result with units that meets the requirements
  6. */
  7. static function fileSizeConv($size, $unit = 'B', $decimals = 1, $targetUnit = 'auto') {
  8. $units = array ('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB');
  9.              $theUnit = array_search(strtoupper($unit), $units); //Initial unit Which
  10.                            // Determine whether to calculate automatically,
  11.                                                                                                                                                                                                    ;= 1024) {
  12.                                                                                                         $size/=1024;
  13.                                         break;                                                                                                                                                                                                                                                                                                                  " "
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn