|
|
|
|
Thread title: PHP: Byes into Megabytes! |
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
01-01-2007, 11:00 PM
|
#1
|
Status: Junior Member
Join date: Oct 2005
Location:
Expertise:
Software:
Posts: 38
|
PHP: Byes into Megabytes!
Hey everyone
Well, i seem to have a slight problem with my current method of working this out (mainly because me and maths dont go well together, lol) see the thing is what i was trying to do is, i have a max amount of space allowed in bytes and how much space is used in bytes so i was trying to work out how much free space the user has left and then tried turning it into megabytes but failed after the output was 26,400 gigabytes :S
can anyone help with this?
thanks in advanced
|
|
01-01-2007, 11:23 PM
|
#2
|
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
Posts: 6,894
|
KB = B*1024
MB = KB*1024
so
mb = b*1024*1024;
|
|
01-01-2007, 11:35 PM
|
#3
|
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
Posts: 5,200
|
Yeah, Google is good for this (using the calculator and unit conversion) but Google is having technical problems and had to shut down the service. When it's back up, it's a good tool for this, just for future reference.
|
|
01-01-2007, 11:37 PM
|
#4
|
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
Posts: 3,820
|
Village Idiot, your mathematics isn't correct since you should be dividing rather than multiplying.
PHP Code:
<?php
$bytes = 10000000000;
// 1024 bytes in a kilobyte
$kb = $bytes / 1024;
// 1024*1024 bytes in a megabyte
// Could use: $mb = $bytes / (1024 * 1024);
$mb = $bytes / pow(1024, 2);
// 1024*1024*1024 bytes in a gigabyte
$gb = $bytes / pow(1024, 3);
// Example of a nicely formatted value
// Second argument is number of decimal places
$pretty_mb = number_format($mb, 2);
$pretty_gb = number_format($gb, 2);
echo $bytes . " bytes<br />\n";
echo $kb . " kilobytes<br />\n";
echo $mb . " megabytes<br />\n";
echo $gb . " gigabytes<br />\n";
echo $pretty_mb . " meg<br />\n";
echo $pretty_gb . " gig<br />\n";
?>
|
|
01-01-2007, 11:38 PM
|
#5
|
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
Posts: 6,894
|
im counting how many bytes are in a given number of MB, you are counting how many MB are in a given number of bytes. most php operations want the input in bytes, not MB or KB
|
|
01-01-2007, 11:58 PM
|
#6
|
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
Posts: 5,200
|
Yeah, yours are wrong.
So if B is equal to 10, then there are 10240 kilobytes in 10 bytes? I'm not sure how that would work, my brain hurts.. and I think I did my setup wrong.
The B should be more than the KB, then KB more than MB, and so on.
|
|
01-02-2007, 12:07 AM
|
#7
|
Status: Junior Member
Join date: Oct 2005
Location:
Expertise:
Software:
Posts: 38
|
wow, guys, didnt expect a response this quickly, after using Salathe's method my current usage appears as
which is kinda long and my max space comes out as
which is correct but php doesnt wanna take the 2 variables away from each other and instead shows nothing, :S, anyone know why?
thanks in advanced
|
|
01-02-2007, 12:18 AM
|
#8
|
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
Posts: 5,200
|
I'm confused on what you mean by "doesnt wanna take the 2 variables away from each other".. care you elaborate?
|
|
01-02-2007, 12:19 AM
|
#9
|
Status: Junior Member
Join date: Oct 2005
Location:
Expertise:
Software:
Posts: 38
|
i turned Salathe's code into a function and then tried this:
PHP Code:
<?php
$megabyte_maxspace = bytesintomegabytes($maxspace);
$megabyte_currentspace = bytesintomegabytes($currentspace);
$usage = $megabyte_currentspace - $megabyte_maxspace;
?>
but the output is 0 which doesnt make sense
|
|
01-02-2007, 12:25 AM
|
#10
|
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
Posts: 3,820
|
Show us your bytesintomegabytes function.
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|