Skip to content

Latest commit

 

History

History
20 lines (19 loc) · 432 Bytes

helper-bytes-for-humans.md

File metadata and controls

20 lines (19 loc) · 432 Bytes

Helper: Bytes for Humans

<?php
if (!function_exists('bytesForHumans')) {
	/**
	 * Bytes to Human Readable
	 * @param $bytes int
	 * @param $formatted bool
	 * @return mixed
	 */
	function bytesForHumans($bytes, $formatted = true){
		$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB'];
		for ($i = 0; $bytes > 1024; $i++) {
			$bytes /= 1024;
		}
		return round($bytes, 2) . ' ' . ($formatted ? $units[$i] : null);
	}
}