Get File Size in reader-friendly units (KB, MB, etc.)

<?php /** * Get the size of a file in reader-friendly units (KB, MB, etc.) * @param string $filename The name of the file to size * @param int $precision The number of decimal places to show (default: 1) * @return string|bool A string with the size of the file or false on error * @author Sunny Walker <www.miraclesalad.com> */ function getFileSize($filename, $precision=1) { $return = false; if (file_exists($filename)) { $fsize = filesize($filename); if ($fsize >= pow(2, 40)) $return = (number_format($fsize / pow(2, 40), $precision)).' TB'; if ($fsize >= pow(2, 30)) $return = (number_format($fsize / pow(2, 30), $precision)).' GB'; if ($fsize >= pow(2, 20)) $return = (number_format($fsize / pow(2, 20), $precision)).' MB'; elseif ($fsize >= 1024) $return = (number_format($fsize / 1024, $precision)).' KB'; else $return = $fsize.' B'; } return $return; } //getFileSize() ?>

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.