Link to home
Start Free TrialLog in
Avatar of noisecouk
noisecouk

asked on

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
Avatar of keteracel
keteracel
Flag of United States of America image

<?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 />";

?>
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 />";

?>
Avatar of noisecouk
noisecouk

ASKER

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";
      }
ASKER CERTIFIED SOLUTION
Avatar of keteracel
keteracel
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Cheers! :)