note 114950 added to function.filesize
| From: | stlawson at joyfulearthtech dot com | Date: | Sun, 04 May 2014 03:53:39 +0000 |
| Subject: | note 114950 added to function.filesize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199404@lists.php.net to get a copy of this message | ||
Here's a more efficient "Human Readable" file size function:
/**
* Returns file size in a more Human Readable format (i.e. truncates larger file
* sizes and adds appropriate suffix, such as KB for "kilobyte" or MB for
"megabyte" etc.
*
* @param type $filesize in Bytes
* @return string
* @throws RuntimeException
* @author S. T. Lawson
*/
function human_readable_filesize($filesize)
{
if (is_nan($filesize))
{
throw new RuntimeException("filesize argument contains NAN!");
}
// Prevent endless loop
$max_step = 8;
// Strip out any commas.
$filesize = str_replace(',','',$filesize);
if (is_numeric($filesize))
{
$divr = 1024; $step = 0;
$suffix =
array('Byte','KB','MB','GB','TB','PB','EB','ZB','YB');
while($filesize >= 1023.99)
{
if ($step++ > $max_step) { throw new RuntimeException("Max # of loop iterations
[$max_step] exceeded while attempting to convert $filesize!"); }
$filesize = $filesize / $divr;
}
return round($filesize, 2).' '.$suffix[$step];
}
else
{
throw new RuntimeException("filesize argument [$filesize] is non-numeric!");
}
}
----
Server IP: 72.52.91.14
Probable Submitter: 68.4.113.112
----
Manual Page -- http://php.net/manual/en/function.filesize.php
Edit -- https://master.php.net/note/edit/114950
Del: integrated -- https://master.php.net/note/delete/114950/integrated
Del: useless -- https://master.php.net/note/delete/114950/useless
Del: bad code -- https://master.php.net/note/delete/114950/bad+code
Del: spam -- https://master.php.net/note/delete/114950/spam
Del: non-english -- https://master.php.net/note/delete/114950/non-english
Del: in docs -- https://master.php.net/note/delete/114950/in+docs
Del: other reasons-- https://master.php.net/note/delete/114950
Reject -- https://master.php.net/note/reject/114950
Search -- https://master.php.net/manage/user-notes.php