Last active
December 17, 2015 00:19
-
-
Save achudars/5520474 to your computer and use it in GitHub Desktop.
Get File Size in reader-friendly units (KB, MB, etc.) using PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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() | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment