Follow Me

Conversion bytes to GB/MB/KB in php

Mon, May 31, 2010

PHP

Simple calculation to convert bytes to gb, mb and kb. In the below example we used round php function to roundup values. Initially started with the number in bytes and then converted it to GB, MB and KB.


<?php
 $byte=1073741824;
if ($byte >= 1073741824)
 $byte_gb = round($byte / 1073741824 * 100) / 100 . " GB";
if ($byte >= 1048576)
 $byte_mb = round($byte / 1048576 * 100) / 100 . " MB";
if ($byte >= 1024)
 $byte_kb = round($byte / 1024 * 100) / 100 . " KB";
else
$byte_byte = $byte . " bytes";
 echo $byte_gb."&nbsp;".$byte_mb."&nbsp;".$byte_kb."&nbsp;".$byte_byte;
?>

Cheers ..

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • blogmarks
  • Design Float
  • DZone
  • MySpace
  • Reddit
  • StumbleUpon
  • Twitter
, , ,

Related Posts:


Recent Posts:

  • Best collection of firefox addons for web designers and developers
  • .htaccess basic features with example
  • how to include an external css and js with javascript dynamically
  • How to preserve line breaks in textarea mysql data
  • how to generate excel report with php and mysql
  • Leave a Reply