note 62656 added to function.filesize

From: Date: Mon, 06 Mar 2006 16:02:02 +0000
Subject: note 62656 added to function.filesize
Groups: php.notes 
Request: Send a blank email to php-notes+get-104910@lists.php.net to get a copy of this message
here a piece of code to format a filesize: <?php function formatbytes($val, $digits = 3, $mode = "SI", $bB = "B"){ //$mode == "SI"|"IEC", $bB == "b"|"B" $si = array("", "k", "M", "G", "T", "P", "E", "Z", "Y"); $iec = array("", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"); switch(strtoupper($mode)) { case "SI" : $factor = 1000; $symbols = $si; break; case "IEC" : $factor = 1024; $symbols = $iec; break; default : $factor = 1000; $symbols = $si; break; } switch($bB) { case "b" : $val *= 8; break; default : $bB = "B"; break; } for($i=0;$i<count($symbols)-1 && $val>=$factor;$i++) $val /= $factor; $p = strpos($val, "."); if($p !== false && $p > $digits) $val = round($val); elseif($p !== false) $val = round($val, $digits-$p); return round($val, $digits) . " " . $symbols[$i] . $bB; } //some test cases: function test($i, $digits = 3, $mode = "SI", $bB = "B"){ echo $i . " = " . formatbytes($i, $digits, $mode, $bB) . "<br>\n"; } test(1024); test(1024*1024); test(1024*1024, 4); test(1024*1024, 3, "IEC"); test(1024, 3, "SI", "b"); test(32423); test(323); test(128, "3", "IEC", "b"); test(324235236362453); test(32423535424236324362453, 3, "IEC"); //actual use: echo formatbytes(file_size("myfile.php")); ?> it formats to bit or bytes according to SI or IEC and rounded to a given number of digits. ---- Server IP: 83.137.20.120 Probable Submitter: 83.116.85.248 ---- X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER, HTML_MESSAGE autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.filesize.php Edit -- http://master.php.net/note/edit/62656 Del: integrated -- http://master.php.net/note/delete/62656/integrated Del: useless -- http://master.php.net/note/delete/62656/useless Del: bad code -- http://master.php.net/note/delete/62656/bad+code Del: spam -- http://master.php.net/note/delete/62656/spam Del: non-english -- http://master.php.net/note/delete/62656/non-english Del: in docs -- http://master.php.net/note/delete/62656/in+docs Del: other reasons-- http://master.php.net/note/delete/62656 Reject -- http://master.php.net/note/reject/62656 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#104910) next »