« Previous entry | Next entry » Browse > Snippets
Skip to comments (7)
convert a size to a unit string in php
Posted by Erik on Jan 13 2006 @ 15:05 :: 3619 unique visits
Converts a number of bytes to a string, using the largest unit.For example:
size(4096) will return '4 KB'
size(512, true) will return '512 bytes'
CODE: PHP
function size($size, $short = false) {
if ($short) {
$unit = array(
'B', 'KB', 'MB', 'GB', 'TB',
'PB', 'EB', 'ZB', 'YB'
);
} else {
$unit = array(
'bytes', 'kilobyte', 'megabyte',
'gigabyte', 'terabyte', 'petabyte',
'exabyte', 'zettabyte', 'yottabyte'
);
}
$base = floor(log($size, 1024));
return round($size / pow(1024, $base), 2).' '.$unit[$base];
}
if ($short) {
$unit = array(
'B', 'KB', 'MB', 'GB', 'TB',
'PB', 'EB', 'ZB', 'YB'
);
} else {
$unit = array(
'bytes', 'kilobyte', 'megabyte',
'gigabyte', 'terabyte', 'petabyte',
'exabyte', 'zettabyte', 'yottabyte'
);
}
$base = floor(log($size, 1024));
return round($size / pow(1024, $base), 2).' '.$unit[$base];
}
7 comments posted so far
Add your own »
2. On Jan 13 2006 @ 18:01 Erik wrote:
Hmm yes that's even better, modified my function accordingly.3. On Jan 17 2006 @ 01:03 dustin wrote:
I use this, but I specify a file instead of a size. I guess I could make things more compact implimenting JohnDoe's function.CODE: PHP
4. On Sep 14 2007 @ 00:10 jesse wrote:
Sometimes I need to get it back into bytes. Of course, this solution requires that you know the tail end of your string. Also multiplying a string is a little loose, but php does alright to keep up.CODE: PHP
function str2size($str){
$ext = array(" Bytes", " KB", " MB", " GB", " TB", " PB");
for($i=0;$i<count($ext);$i++){
if(preg_match("/(\d+(?:\.(?:\d+)?)?){$ext[$i]}$/",$str,$matches)==true){
return round(pow(1024,$i)*$matches[1]);
}
}
}
$ext = array(" Bytes", " KB", " MB", " GB", " TB", " PB");
for($i=0;$i<count($ext);$i++){
if(preg_match("/(\d+(?:\.(?:\d+)?)?){$ext[$i]}$/",$str,$matches)==true){
return round(pow(1024,$i)*$matches[1]);
}
}
}
5. On Jun 03 2009 @ 01:08 guest wrote:
This is very useful information here. My wife was well on my way to losing weight before she became pregnant<br/>, now she is gaining weight like crazy and battling smelly gas<br/>. This happens to many people whether you are bbw women or a regular sized woman. After the baby is born she will go back to working out five days a week. Hopeful I won't have to find a minimum wage job. If you are 14 years old you can also start working for minimum wage, but hopefully it would be a lot more. If you are a 13 year old girl or boy you will have to work off the books.Are you morbidly obese? Have you ever considered weight loss surgery. Some people believe that it is the best way to lose weight, while others are totally against it. It really depends on what you are willing to do before and after surgery. It is not a magic weight loss pill that will do all of the work for you. But if you try, there are many ways that you can lose weight and keep it off.
If you live in the United States or anywhere for that matter, you can find apartments for rent in the UK or abroad. Where can I find summer jobs for teens? You can check how to start a website online or your local paper. People are looking to rent their homes, sublet their expensive apartments, and even take on roommates to help save some money right now. It is definitely a buyer's market.
6. On Jul 14 2009 @ 07:33 guest wrote:
AVI to DVD Converter,AVI to DVD Creator,iPhone Ringtone Maker for Mac,AVI Converter OS X,VOB Converter OS X,AVCHD Video Converter,FLV Converter,PowerPoint Converter,AVCHD Converter,Blue-Ray ripper,Rip Blue Ray,FLV to MOV Mac,VOB to DVD,HD Video Converter,iPod Playlist Transfer7. On Jan 05 2010 @ 14:27 uggbaileybutton wrote:
bailey button uggs-ugg boots cheap
ugg boots uk
ugg classic
1. On Jan 13 2006 @ 15:50 TheJohnDoe2005 wrote:
This is for PHP...I have been using it for my download manager to convert bytes.{
$filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB");
return round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i];
}