Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1426 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

PHP: Byes into Megabytes!

Thread title: PHP: Byes into Megabytes!
Closed Thread  
Page 1 of 2 1 2 >
    Thread tools Search this thread Display Modes  
01-01-2007, 11:00 PM
#1
WinSrev is offline WinSrev
WinSrev's Avatar
Status: Junior Member
Join date: Oct 2005
Location:
Expertise:
Software:
 
Posts: 38
iTrader: 0 / 0%
 

WinSrev is on a distinguished road

  Old  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
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,894
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

KB = B*1024
MB = KB*1024

so
mb = b*1024*1024;

01-01-2007, 11:35 PM
#3
Andrew R is offline Andrew R
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
 
Posts: 5,200
iTrader: 17 / 95%
 

Andrew R is on a distinguished road

  Old

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
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

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(10242);

// 1024*1024*1024 bytes in a gigabyte
$gb $bytes pow(10243);

// Example of a nicely formatted value
// Second argument is number of decimal places
$pretty_mb number_format($mb2);
$pretty_gb number_format($gb2);

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
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,894
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

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
Andrew R is offline Andrew R
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
 
Posts: 5,200
iTrader: 17 / 95%
 

Andrew R is on a distinguished road

  Old

Yeah, yours are wrong.

KB = B*1024
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
WinSrev is offline WinSrev
WinSrev's Avatar
Status: Junior Member
Join date: Oct 2005
Location:
Expertise:
Software:
 
Posts: 38
iTrader: 0 / 0%
 

WinSrev is on a distinguished road

  Old

wow, guys, didnt expect a response this quickly, after using Salathe's method my current usage appears as

0.119703292847 megabytes
which is kinda long and my max space comes out as

25 megabytes
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
Andrew R is offline Andrew R
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
 
Posts: 5,200
iTrader: 17 / 95%
 

Andrew R is on a distinguished road

  Old

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
WinSrev is offline WinSrev
WinSrev's Avatar
Status: Junior Member
Join date: Oct 2005
Location:
Expertise:
Software:
 
Posts: 38
iTrader: 0 / 0%
 

WinSrev is on a distinguished road

  Old

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
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

Show us your bytesintomegabytes function.

Closed Thread  
Page 1 of 2 1 2 >


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed