PHP convert interger bytes to either bytes, Kb, or Mb depending on size

Hi there, I'm trying to create a simple script within PHP that gets an interger from the variable $size and converts this number into another variable $size2 which is either a value of bytes if less than 1024, kb if between 1024 and 1024*1024 and mb for anything greater.

//Rough code so far, returns a T_Variable error
if ($size > 1024 && < 1024*1024 ){
int $size2==($size/1024);
$size3== "$size2 kb";
}

Help writing the script will be very much appreciated. Cheers
noisecoukAsked:
Who is Participating?
 
keteracelCommented:
so you just need to use the number_format function: http://uk.php.net/manual/en/function.number-format.php

like so:

      if($size < 1024){
      $size3="$size bytes";
      }
      if($size > 1024 && $size < 1048576){
      $size2= number_format($size/1024,2);
      $size3="$size2 Kb";
      }
      if($size > 1048576){
      $size2= number_format($size/1048576,2);
      $size3="$size2 Mb";
      }
0
 
keteracelCommented:
<?php

function getBytesString($number) {
  $thousandArray = array();
  $thousandArray[0] = '';
  $thousandArray[1] = 'K';
  $thousandArray[2] = 'M';
  $thousandArray[3] = 'G';
  $thousandArray[4] = 'T';

  for ($i = 0; $number > 1024 && $i < count($thousandArray); $i++) {
    $number /= 1024;
  }
  return number_format($number,2).' '.$thousandArray[$i].'b';
}

echo getBytesString(1024)."<br />";
echo getBytesString(1025)."<br />";
echo getBytesString((1024*1024)+1)."<br />";
echo getBytesString((1024*1024*1024)+1)."<br />";
echo getBytesString((1024*1024*1024*1024)+1)."<br />";

?>
0
 
keteracelCommented:
this one goes up to pica and allows you to define whether it's bytes or bits:

<?php

function getBytesString($number, $bytes=true) {
  $bitsOrBytes = ($bytes)?'B':'b';

  $thousandArray = array();
  $thousandArray[0] = '';
  $thousandArray[1] = 'K';
  $thousandArray[2] = 'M';
  $thousandArray[3] = 'G';
  $thousandArray[4] = 'T';
  $thousandArray[5] = 'P';

  for ($i = 0; $number > 1024 && $i < count($thousandArray); $i++) {
    $number /= 1024;
  }
  return number_format($number,2).' '.$thousandArray[$i].$bitsOrBytes;
}

echo getBytesString(1024, false)."<br />";
echo getBytesString(1024)."<br />";
echo getBytesString(1025)."<br />";
echo getBytesString((1024*1024)+1)."<br />";
echo getBytesString((1024*1024*1024)+1)."<br />";
echo getBytesString((1024*1024*1024*1024)+1)."<br />";
echo getBytesString((1024*1024*1024*1024*1024)+1)."<br />";
echo getBytesString((1024*1024*1024*1024*1024*1024)+1)."<br />";

?>
0
 
noisecoukAuthor Commented:
Haha! I don't think I'll be uploading files any bigger than a couple of Mb. Think of the bandwidth is a couple of people download Pica byte files!!

I've decided to use my script which works here as I only need it to go up to Mb. Is there any I can output the value of Mb so it leaves me with 2 decimal places?


      if($size < 1024){
      $size3="$size bytes";
      }
      if($size > 1024 && $size < 1048576){
      $size2= floor($size/1024);
      $size3="$size2 Kb";
      }
      if($size > 1048576){
      $size2= floor($size/1048576);
      $size3="$size2 Mb";
      }
0
 
noisecoukAuthor Commented:
Cheers! :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.