Convert Bytes Into KB, MB And GB Using PHP

Tuesday, Jan 24, 2017

Tags:- PHP HTML


In this tutorial we will show you how to convert bytes to kilobytes(kb), Megabytes(MB) and Gigabytes(GB) using PHP with our simple and easy function you can also easily use this code in making bigger applications.You may also like convert seconds into minutes and hours.


See Demo
Convert Bytes Into KB, MB And GB Using PHP


To Convert Bytes It Takes Only two Steps:-

  1. Make a PHP file and define markup and scripting
  2. Make a CSS file and define styling


Step 1.Make a PHP file and define markup and scripting

We make a PHP file and save it with a name convert.php

<?php
function convert($size,$unit) 
{
 if($unit == "KB")
 {
  return $fileSize = round($size / 1024,4) . 'KB';	
 }
 if($unit == "MB")
 {
  return $fileSize = round($size / 1024 / 1024,4) . 'MB';	
 }
 if($unit == "GB")
 {
  return $fileSize = round($size / 1024 / 1024 / 1024,4) . 'GB';	
 }
}

if(isset($_POST['convert_size']))
{
 $size=$_POST['size'];
 $unit=$_POST['convert_unit'];
 $size=convert($size,$unit);
}
?>

<html>
<head>
<link type="text/css" rel="stylesheet" href="convert_style.css"/>
</head>
<body>
<div id="wrapper">

<div id="convert_div">
 <form method="post"action="">
  <input type="text" name="size" placeholder="Enter Bytes">
  <select name="convert_unit">
   <option>KB</option>
   <option>MB</option>
   <option>GB</option>
  </select>
  <br>
  <input type="submit" name="convert_size" value="Convert Bytes">
 </form>
 <p><?php echo $size;?></p>
</div>

</div>
</body>
</html>

In this step we create a form to enter bytes and select unit to convert on.After submitting the form we get the value of size and units and call convert function to convert bytes into particular unit and display in browser.You may also like convert currency using PHP.



Step 2.Make a CSS file and define styling

We make a CSS file and save it with a name convert_style.css

body
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:100%;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
 background-color:#A9BCF5;
}
#wrapper
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:995px;
}
#wrapper h1
{
 margin-top:50px;
 font-size:45px;
 color:#045FB4;
}
#wrapper h1 p
{
 font-size:18px;
}
#convert_div input[type="text"]
{
 width:400px;
 height:55px;
 padding-left:10px;
 font-size:18px;
 margin-bottom:15px;
 border:none;
 color:#045FB4;
 font-weight:bold;
}
#convert_div select
{
 margin:0px;
 padding:0px;
 border:none;
 width:70px;
 height:55px;
 margin-bottom:15px;
 border:none;
 color:#045FB4;
 font-weight:bold;
}
#convert_div input[type="submit"]
{
 width:330px;
 height:45px;
 font-size:16px;
 font-weight:bold;
 background-color:#5882FA;
 color:white;
 border:none;
 box-shadow:0px 4px 0px 0px #2E64FE;
 border-radius:3px;
}

That's all, this is how to convert bytes into kb, mb and gb using PHP.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email