note 85482 deleted from function.filesize by aidan

From: Date: Wed, 15 Apr 2009 17:37:24 +0000
Subject: note 85482 deleted from function.filesize by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-153353@lists.php.net to get a copy of this message
Note Submitter: d.abromeit ---- yes, this function is well known and great - but if there are values <=0, then you'll get "warning: division by zero ...". happens if filesize() returns false on error. so better use something like this: <?php function byte_convert($bytes) { $symbol = array('B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'); $exp = 0; $converted_value = 0; if( $bytes > 0 ) { $exp = floor( log($bytes)/log(1024) ); $converted_value = ( $bytes/pow(1024,floor($exp)) ); } return sprintf( '%.2f '.$symbol[$exp], $converted_value ); } ?> it returns "0" in our problem cases. you may add "false" for backend-usage also. ;O)

« previous php.notes (#153353) next »