Skip to content

Instantly share code, notes, and snippets.

@jrobinsonc

jrobinsonc/README.md

Last active Dec 15, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.