Parsing and unparsing file sizes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Kasai
Forum Newbie
Posts: 13
Joined: Sun Jul 15, 2007 11:27 am

Parsing and unparsing file sizes

Post by Kasai »

Hey guys,

I'm writing a webpage that has upload capabilities. However, i'm trying to restrict each user's total amount they are allowed to upload. I have decided that the best way is to have a database column with a max limit. Then when they log on or try to upload something, the code will check their total filesize with the databases max and either allow or deny certain functions. However, im having trouble understanding parsed file sizes.

at this point, i can grab the total size of all the files in a certain directory and i hvae it storage as $size. then i put it into a session variable. However, the number that the code spits out is like a 5 digit unparsed number. I then use parse_file_size() and it makes the code into the pretty 7.00 MB . However, when doing arithmetic operations, the 7.00 is what it uses. So what if i were to have someone upload a GB? that would be 1.00 GB and the code would allow if i checked if it was greater that 7.00 MB because the number is smaller.

So my question is this. Is there any way to understand what the parsed number means so that i can use it to do arithmatic operations, or is there like different operations so that i can check the GB vs MB?

Or, am i totally going about this the wrong way?

here is the number unparsed : 8373452
and here it is after passing through parse_file_size() : 7.99 MB

Thanks
User avatar
yacahuma
Forum Regular
Posts: 870
Joined: Sun Jul 01, 2007 7:11 am

just use the byte information

Post by yacahuma »

just is the number of bytes (the long number) for everything
The MB stuff should be for user display only
Kasai
Forum Newbie
Posts: 13
Joined: Sun Jul 15, 2007 11:27 am

Post by Kasai »

so the unparsed number is the number of bytes?

and if so, why are there 8 million some odd instead of like a 7 million 990thousand?
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

1kb = 1024 bytes
1mb = 1024kb
1gb = 1024mb

not 1000... 1024
Kasai
Forum Newbie
Posts: 13
Joined: Sun Jul 15, 2007 11:27 am

Post by Kasai »

oh man. im dumb. i forgot eveything is in base 2.lol

thanks guys. i should have this up very soon.
Post Reply