Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.0.0

Request #2798 function for human readable filesize conversion
Submitted: 2004-11-19 20:13 UTC
From: cweiske Assigned: cweiske
Status: Closed Package: File_Util
PHP Version: - OS: -
Roadmaps: (Not assigned)    

 [2004-11-19 20:13 UTC] cweiske
Description: ------------ While programming a file browser, I came to the problem of displaying the filesize in a human-readable format. There are numerous 20-liners out there which do the thing, but not in PEAR. To my mind, the File package would be the right place for such a function. Could you include the function in the package? Reproduce code: --------------- /** * returns the filesize using a prefix * like "kilo", "mebi" or "giga" * @author Christian Weiske <> * * @param int The size to convert * @param int The number of decimals to use * @param boolean Use long names (kilobyte) instead of short ones (kB) * @param boolean If the old style should be used * @param boolean If the "BiBytes" names should be used [applies only to !$bOldStyle] * * @return string The filesize in human readable format */ function prefixed($nSize, $nDecimals = 1, $bLong = false, $bOldStyle = true, $bUseBiBytes = true) { $nBase = ($bOldStyle || $bUseBiBytes) ? 1024 : 1000; $arNames = array('', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zetta', 'yotta'); $nMax = count($arNames) - 1; for ($nA = 0; $nSize >= $nBase && $nA < $nMax; $nA++) { $nSize /= $nBase; } $strName = ($bOldStyle || !$bUseBiBytes) ? $arNames[$nA] : substr($arNames[$nA],0,2) . 'bi'; if (!$bLong) { $strName = $bOldStyle || !$bUseBiBytes ? substr($strName, 0, 1) : substr($strName, 0, 2); $strName .= 'B'; $strName = $nA >= 2 ? strtoupper($strName) : $strName; } else { $strName .= $nSize == 1 ? 'byte' : 'bytes'; } return round($nSize,$nDecimals) . ' ' . $strName; }


 [2004-11-20 09:25 UTC] cweiske
There is a small bug in the code. As I don't want to post the whole code again, please contact me if you plan to include this in the class
 [2004-12-30 13:48 UTC] mike
Please post the corrected patch or email it to me, but: - without hungarian notation - please specify the var names in @param Thanks a lot, Mike
 [2005-06-17 11:18 UTC] mike
No feedback was provided. The bug is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so and change the status of the bug back to "Open". Thank you.
 [2006-06-03 10:27 UTC] aidan (Aidan Lister)
Found this bug accidently on googly, if you still want to include a function like this in the File class:
 [2009-08-06 12:56 UTC] cweiske (Christian Weiske)
The following patch has been added/updated: Patch Name: readable.diff Revision: 1249545402 URL:
 [2009-08-06 12:57 UTC] cweiske (Christian Weiske)
The following patch has been added/updated: Patch Name: human-readabe.phpt Revision: 1249545423 URL:
 [2010-04-08 07:01 UTC] samwilson (Sam Wilson)
I am using a variation of Christian's function, with the following header and signature: /** * Returns a file's size with a human-readable suffix such as "kilobytes", "MB", * "mebibytes", "TB", etc. * * @param string|integer|array $file The name of the file, or a file size in * bytes (or an array of these). * @param integer $decimals The precision or number of digits after the * decimal point. * @param boolean $long Use long names (kilobyte) and a space * instead of short ones (kB) and no space. * @param boolean $useSI Whether units should be multiples of * 1000 (true) or 1024 (false). * @param boolean $useBiBytes If the "BiBytes" names should be used * (applies only when $useSI = true). * * @return string|array The filesize in human readable format (or array of same). * @access public * @static */ function filesize($file, $decimals = 2, $long = false, $useSI = true, $useBiBytes = true) { /* ... */ } I will submit a patch if there is agreement about these changes (i.e. different parameters and defaults; the function still does the same thing).
 [2010-04-09 16:14 UTC] cweiske (Christian Weiske)
please add the patch.
 [2011-03-12 05:09 UTC] dufuz (Helgi Þormar Þorbjörnsson)
-Package: File +Package: File_Util -Assigned To: +Assigned To: cweiske
 [2011-03-12 05:13 UTC] dufuz (Helgi Þormar Þorbjörnsson)
-Status: Assigned +Status: Closed
This bug has been fixed in SVN. If this was a documentation problem, the fix will appear on by the end of next Sunday (CET). If this was a problem with the website, the change should be live shortly. Otherwise, the fix will appear in the package's next release. Thank you for the report and for helping us make PEAR better.