TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-27-2007, 04:01 PM   #1 (permalink)
The Frequenter
 
Join Date: Dec 2007
Location: In my basement
Posts: 386
Thanks: 47
Aaron is on a distinguished road
Default Dumb question...

When I do any php function that gets a size of something, does it measure the size in bytes? bits?

and the size ratio is like this, correct?:
1 kilobyte (KB) 1,024 bytes
1 megabyte (MB) 1,048,576 bytes
1 gigabyte (GB) 1,073,741,824 bytes
1 terabyte (TB) 1,099,511,627,776 bytes
1 petabyte (PB) 1,125,899,906,842,624 bytes

can also be seen as this:
Bit: Smallest unit of measurement.
Byte: 8 bits
Kilobyte: (KB) 1024 bytes.
MegaByte: (MB) 1024 kilobytes (KB)
GigaByte: (GB) 1024 megabytes (MB)

but is the size really important? Could you round it down and say 1kb = 1000 bytes? Does PHP have a built in function to convert these values, or do I have to write all that stuff?
Send a message via MSN to Aaron
Aaron is offline  
Reply With Quote
Old 12-27-2007, 04:17 PM   #2 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

No, PHP doesn't have what you want, but you could easily write your own functions (it's actually pretty easy). All you have to do is some basic math. Every next measurement unit is the previous measurement unit * 1024.

- 1 kilobyte (KB) = 1,024 bytes
- 1 megabyte (MB) = 1 KB * 1024 = 1024 * 1024 = 1,048,576 bytes
- 1 gigabyte (GB) = 1MB * 1024 = ( 1024 * 1024 ) * 1024 = 1,073,741,824 bytes

And no, you can't 'just round down the values to 1000'. In programming, that doesn't work - by doesn't work I mean you won't get the right values. In marketing, it does (see the storage devices).
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 12-27-2007, 10:12 PM   #3 (permalink)
The Contributor
 
Join Date: Apr 2005
Location: Kent, UK
Posts: 54
Thanks: 0
Dr John is on a distinguished road
Default

Does it measure the size in bytes??? The size of WHAT? Depends what your function is going to be used for - it could be pixels, it could be inches, it could be light years, it could be moles - it's your function, you decide.
__________________
www.kidneydialysis.org.uk
Dr John is offline  
Reply With Quote
Old 12-27-2007, 10:55 PM   #4 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by Aaron View Post
When I do any php function that gets a size of something, does it measure the size in bytes? bits?
Functions like filesize return the size in bytes. The return values of all PHP functions are available in the PHP manual (there's always a section called "Return Values" with an explanation).

Quote:
Originally Posted by Aaron View Post
Does PHP have a built in function to convert these values, or do I have to write all that stuff?
It is very simple mathematics, essentially. There are hundreds of available functions available throughout the 'net which will take a byte value and convert it into any format that you want... but as a learning process I would recommend writing your own to do exactly what you want. All that are really needed are / (divide), (int) (type casting/juggling) or intval() (convert number to integer) -- or if you're feeling confident you could use shifting bitwise operators ;)

Also, on the topic of 1000 bytes versus 1024 bytes, two different systems are available for use. Using SI prefix, 1,000,000 bytes equals 1 megabyte (MB); using Binary prefix, 1,048,576 (1024 * 1024) bytes equals 1 mebibyte (MiB). Often, notably, the SI prefixes are used for both instances with the precise meaning being determined by the context in which the value is used! More information on this ambiguity can be found on Wikipedia.
Salathe is offline  
Reply With Quote
Old 12-29-2007, 10:16 PM   #5 (permalink)
Alan @ CIT
Member of the Month
The Frequenter
Member of the Month Top Contributor 
 
Alan @ CIT's Avatar
 
Join Date: Apr 2005
Location: South UK
Posts: 483
Thanks: 51
Alan @ CIT is on a distinguished road
Default

Here is a function I've used on a few projects (because I'm too lazy to write my own :D) which does a fine job of converting a filesize() result (bytes) to a "human readable" size

PHP: filesize - Manual

Alan.
Send a message via MSN to Alan @ CIT
Alan @ CIT is offline  
Reply With Quote
Reply



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

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 06:31 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design