note 54712 added to function.filesize

From: Date: Wed, 13 Jul 2005 12:52:53 +0000
Subject: note 54712 added to function.filesize
Groups: php.notes 
Request: Send a blank email to php-notes+get-91892@lists.php.net to get a copy of this message
Here my more future-save and much smaller function to convert bytes into a readable size: <?php /** * function filesize_format() - version 1.0 * Formate a filesize to a human readable format * * @param integer bytes * @param string the decimal delimiter, default '.' * @param string the spacer between the filesize and the unit, default ' ' * @param boolean if set to true, all units are written in lowercase, default false * * @return string the formated filesize * * @access public * * @author DASPRiD <d@sprid.de> */ function filesize_format ($bytes, $decimal='.', $spacer=' ', $lowercase=false) { $bytes = max(0, (int)$bytes); $units = array('YB' => 1208925819614629174706176, // yottabyte 'ZB' => 1180591620717411303424, // zettabyte 'EB' => 1152921504606846976, // exabyte 'PB' => 1125899906842624, // petabyte 'TB' => 1099511627776, // terabyte 'GB' => 1073741824, // gigabyte 'MB' => 1048576, // megabyte 'KB' => 1024, // kilobyte 'B' => 0); // byte foreach ($units as $unit => $qty) { if ($bytes >= $qty) { $broken_number = explode('.', $number); if (!$broken_number[1]) return number_format(!$qty ? $bytes: $bytes /= $qty, 0, $decimal).$spacer.$unit; else return number_format(!$qty ? $bytes: $bytes /= $qty, 0, $decimal).$decimal.substr($broken_number[1], 0, 2).$spacer.$unit; } } } } // end of 'filesize_format' function(); ?> ---- Manual Page -- http://www.php.net/manual/en/function.filesize.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54712 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54712&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54712&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54712&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54712&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54712&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54712&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54712&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91892) next »