Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

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

Posted on 2007-08-07
5
Medium Priority
?
3,272 Views
Last Modified: 2013-12-13
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
0
Comment
Question by:noisecouk
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:keteracel
ID: 19644553
<?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
 
LVL 9

Expert Comment

by:keteracel
ID: 19644652
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
 

Author Comment

by:noisecouk
ID: 19644898
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
 
LVL 9

Accepted Solution

by:
keteracel earned 2000 total points
ID: 19644949
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
 

Author Comment

by:noisecouk
ID: 19645246
Cheers! :)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question