Skip to content

PHP: Dateigröße formatieren

von dennis

Das Original ist hier zu finden: http://php.net/manual/de/function.filesize.php

function formatBytes($b,$p = null) {
	/**
	* returns formatted number of bytes.
	* two parameters: the bytes and the precision (optional).
	* if no precision is set, function will determine clean
	* result automatically.
	**/
	$units = array("B","KiB","MiB","GiB");
	$r = array('bytes'=>0,'units'=>'');
	$c=0;
	if(!$p && $p !== 0) {
		foreach($units as $k => $u) {
			if(($b / pow(1024,$k)) >= 1) {
				$r['bytes'] = $b / pow(1024,$k);
				$r['units'] = $u;
				$c++;
			}
		}
		return number_format($r['bytes'],2) . " " . $r['units'];
	} else {
		return number_format($b / pow(1024,$p)) . " " . $units[$p];
	}
}

Aus → PHP

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS

*