...

View Full Version : Resolved converting kilobytes to megabytes



cosmicsea
01-23-2010, 11:31 PM
hi i wondering if there is a way to make some text on the screen change from kilobytes to megabytes. lets say on a page it says 99857 KB is there some kind of replace variable or anything that would automatically convert that number to like 99mb instead automatically? this seems like it would be something complicated to me. to clarify im talking about something that would automatically change random instances of different amounts of kilobytes. lets say we have a list of

12323kb
32463kb
34353kb
34256kb
and convert those to megabytes.

cosmicsea
01-23-2010, 11:34 PM
i use a code like this
<?php print($data['size']); ?> and it will automatically show the size of whatever its connected to in kilobytes.

Jedi Knight
01-23-2010, 11:39 PM
I use this to determine the size of files that have been uploaded to my site.



$filesize = filesize($filename);
$filesize = ($filesize / 1048576);
if ($filesize < 1) {
$filesize = round($filesize*1024,0);
$check1 = "KB";
} else {
$filesize = round($filesize,2);
$fsizetxt = "".$fsize." MB";
$check1 = "MB";
}
echo round($filesize,2).$check1;

cosmicsea
01-23-2010, 11:45 PM
I use this to determine the size of files that have been uploaded to my site.



$filesize = filesize($filename);
$filesize = ($filesize / 1048576);
if ($filesize < 1) {
$filesize = round($filesize*1024,0);
$check1 = "KB";
} else {
$filesize = round($filesize,2);
$fsizetxt = "".$fsize." MB";
$check1 = "MB";
}
echo round($filesize,2).$check1;


yeah but it there a way to make it grab the KB info from the code i use and replace it with megabytes?

ninnypants
01-24-2010, 12:02 AM
well if $data['size'] contains the size couldn't you just do something like this.


$size = $data['size'];
$size = preg_replace("#[\D]#i", '', $size);// strip the string down to just numbers
$size = round($size/1024);// convert and round to the nearest MB
echo $size.'MB';

cosmicsea
01-24-2010, 12:10 AM
well if $data['size'] contains the size couldn't you just do something like this.


$size = $data['size'];
$size = preg_replace("#[\D]#i", '', $size);// strip the string down to just numbers
$size = round($size/1024);// convert and round to the nearest MB
echo $size.'MB';


yes that does work, thanks alot!:thumbsup:

Jedi Knight
01-24-2010, 12:20 AM
That should work if you are only dealing with MB.
The code I have will display as KB for files under 1 MB, and then MB for all files over 1 MB.
I guess it depends on what it is used for.

Try this one.



$filesize = $data['size'];
$filesize = ($filesize / 1048576);
if ($filesize < 1) {
$filesize = round($filesize*1024,0);
$check1 = "KB";
} else {
$filesize = round($filesize,2);
$fsizetxt = "".$fsize." MB";
$check1 = "MB";
}
echo round($filesize,2).$check1;


Sorry, I didn't see your reply.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum