jrobinsonc
4/6/2013 - 4:54 AM

Convert from Bytes to KB, MB, GB, TB, PB, EB, ZB, YB. #php #files

Convert from Bytes to KB, MB, GB, TB, PB, EB, ZB, YB. #php #files

<?php
 
/**
 * Convert from Bytes to KB, MB, GB, TB, PB, EB, ZB, YB.
 * 
 * @author JoseRobinson.com
 * @link GitHup: https://gist.github.com/5324872
 * @version 201304062326
 * @param int $bytes
 * @return string
 */
function byte_convert($bytes)
{
    $symbol = array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
    $exp = floor(log($bytes) / log(1024));

    return sprintf('%.2f ' . $symbol[$exp], ($bytes / pow(1024, floor($exp))));
}

Convert from Bytes to other file size units.

Convert from Bytes to KB, MB, GB, TB, PB, EB, ZB, YB.

Usage

require 'byte_convert.php';

// Get file size in bytes.
$file_size = filesize('/path/to/music/dj-zoso.mp3');

// Print the size in a human readable unit.
printf('File size: %s', byte_convert($file_size));