Skip to content

Instantly share code, notes, and snippets.

@jrobinsonc
Last active December 15, 2015 21:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrobinsonc/5324872 to your computer and use it in GitHub Desktop.
Save jrobinsonc/5324872 to your computer and use it in GitHub Desktop.
Convert from Bytes to KB, MB, GB, TB, PB, EB, ZB, YB. #php #files

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));
<?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))));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment